我有一个承载 DialogFragment 的 FragmentActivity。
DialogFragment 执行网络请求并处理 Facebook 身份验证,因此我需要在轮换期间保留它。
我已经阅读了与此问题相关的所有其他问题,但没有一个真正解决了这个问题。
我正在使用 putFragment 和 getFragment 来保存 Fragment 实例并在重新创建活动期间再次获取它。
但是,我总是在 onRestoreInstanceState 中调用 getFragment 时遇到空指针异常。我还想防止对话框在旋转过程中被关闭,但到目前为止我什至无法保留它的实例。
任何想法出了什么问题?
这是我的代码目前的样子:
public class OKLoginActivity extends FragmentActivity implements OKLoginDialogListener
{
private OKLoginFragment loginDialog;
private static final String TAG_LOGINFRAGMENT = "OKLoginFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
if(savedInstanceState == null)
{
loginDialog = new OKLoginFragment();
loginDialog.show(fm, TAG_LOGINFRAGMENT);
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
getSupportFragmentManager().putFragment(outState,TAG_LOGINFRAGMENT, loginDialog);
}
@Override
public void onRestoreInstanceState(Bundle inState)
{
FragmentManager fm = getSupportFragmentManager();
loginDialog = (OKLoginFragment) fm.getFragment(inState, TAG_LOGINFRAGMENT);
}
}
这是异常堆栈跟踪:
02-01 16:31:13.684: E/AndroidRuntime(9739): FATAL EXCEPTION: main
02-01 16:31:13.684: E/AndroidRuntime(9739): java.lang.RuntimeException: Unable to start activity ComponentInfo{io.openkit.example.sampleokapp/io.openkit.OKLoginActivity}: java.lang.NullPointerException
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.access$700(ActivityThread.java:141)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.os.Looper.loop(Looper.java:137)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-01 16:31:13.684: E/AndroidRuntime(9739): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 16:31:13.684: E/AndroidRuntime(9739): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 16:31:13.684: E/AndroidRuntime(9739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-01 16:31:13.684: E/AndroidRuntime(9739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-01 16:31:13.684: E/AndroidRuntime(9739): at dalvik.system.NativeStart.main(Native Method)
02-01 16:31:13.684: E/AndroidRuntime(9739): Caused by: java.lang.NullPointerException
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:528)
02-01 16:31:13.684: E/AndroidRuntime(9739): at io.openkit.OKLoginActivity.onRestoreInstanceState(OKLoginActivity.java:62)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.Activity.performRestoreInstanceState(Activity.java:910)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1131)
02-01 16:31:13.684: E/AndroidRuntime(9739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)