我试图掌握保存 Android 应用程序并在恢复时恢复它的简单方面。我已经到处寻找答案,但没有任何效果。由于我正在开发的应用程序遇到问题,我将其归结为下面的测试应用程序。我在某处得到一个空指针。我有一个要加载的基本 HTML 页面。当我旋转手机时,它会尝试保存但无法正确恢复。我在这里想念什么?
这是我的活动:
package com.example.saverestore;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
if(savedInstanceState == null || savedInstanceState.isEmpty())
{
Log.d("RGiTest", "super.loadUrl");
super.loadUrl("file:///android_asset/www/index.html");
}
else
{
Log.d("RGiTest", "Restoring the state");
this.appView.restoreState(savedInstanceState);
}
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
this.appView.saveState(outState);
}
}
这是我的 index.html:
<!DOCTYPE HTML>
<html>
<head>
<title>Save Restore</title>
</head>
<body style="background-color: #000000">
<h1 style="margin-left: 40px; color: #FF0000;">Test Save/Restore</h1>
</body>
</html>
旋转手机时出现此错误:
01-31 14:18:31.707: D/DroidGap(3129): onMessage(onPageStarted,file:///android_asset/www/index.html)
01-31 14:18:31.726: D/Cordova(3129): onPageFinished(file:///android_asset/www/index.html)
01-31 14:20:15.183: D/AndroidRuntime(3129): Shutting down VM
01-31 14:20:15.183: W/dalvikvm(3129): threadid=1: thread exiting with uncaught exception (group=0x40015578)
01-31 14:20:15.246: E/AndroidRuntime(3129): FATAL EXCEPTION: main
01-31 14:20:15.246: E/AndroidRuntime(3129): java.lang.NullPointerException
01-31 14:20:15.246: E/AndroidRuntime(3129): at java.lang.String.indexOf(String.java:1145)
01-31 14:20:15.246: E/AndroidRuntime(3129): at org.apache.cordova.CordovaWebView.loadUrlNow(CordovaWebView.java:517)
01-31 14:20:15.246: E/AndroidRuntime(3129): at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:402)
01-31 14:20:15.246: E/AndroidRuntime(3129): at org.apache.cordova.CordovaWebViewClient.onPageFinished(CordovaWebViewClient.java:298)
01-31 14:20:15.246: E/AndroidRuntime(3129): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:282)
01-31 14:20:15.246: E/AndroidRuntime(3129): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 14:20:15.246: E/AndroidRuntime(3129): at android.os.Looper.loop(Looper.java:130)
01-31 14:20:15.246: E/AndroidRuntime(3129): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-31 14:20:15.246: E/AndroidRuntime(3129): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 14:20:15.246: E/AndroidRuntime(3129): at java.lang.reflect.Method.invoke(Method.java:507)
01-31 14:20:15.246: E/AndroidRuntime(3129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-31 14:20:15.246: E/AndroidRuntime(3129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-31 14:20:15.246: E/AndroidRuntime(3129): at dalvik.system.NativeStart.main(Native Method)
有人可以就此提供建议或指导吗?
谢谢