我在我的活动中使用动态片段,这是我的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_utente);
if(findViewById(R.id.framelayout2) != null){
mDualFragments=true;
ListUtente lu = new ListUtente();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.framelayout1, lu);
transaction.commit();
DettUtente du = new DettUtente();
FragmentTransaction transaction1 = getSupportFragmentManager()
.beginTransaction();
transaction1.add(R.id.framelayout2, du);
transaction1.commit();
} else{
ListUtente lu = new ListUtente();
lu.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.framelayout1, lu).commit();
}
}
我的问题是当我旋转设备时:
03-05 18:26:47.223: E/AndroidRuntime(14216): FATAL EXCEPTION: main
03-05 18:26:47.223: E/AndroidRuntime(14216): java.lang.RuntimeException: Unable to destroy activity {com.unipg.utente/com.unipg.utente.ActivityUtente}: java.lang.NullPointerException
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3401)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3419)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3617)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.access$700(ActivityThread.java:144)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.os.Looper.loop(Looper.java:137)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.main(ActivityThread.java:4966)
03-05 18:26:47.223: E/AndroidRuntime(14216): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 18:26:47.223: E/AndroidRuntime(14216): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 18:26:47.223: E/AndroidRuntime(14216): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-05 18:26:47.223: E/AndroidRuntime(14216): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-05 18:26:47.223: E/AndroidRuntime(14216): at dalvik.system.NativeStart.main(Native Method)
03-05 18:26:47.223: E/AndroidRuntime(14216): Caused by: java.lang.NullPointerException
03-05 18:26:47.223: E/AndroidRuntime(14216): at com.unipg.utente.ListUtente.onDestroy(ListUtente.java:111)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.Fragment.performDestroy(Fragment.java:1680)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1898)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:324)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.Activity.performDestroy(Activity.java:5172)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109)
03-05 18:26:47.223: E/AndroidRuntime(14216): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3388)
03-05 18:26:47.223: E/AndroidRuntime(14216): ... 12 more
03-05 18:30:31.543: E/Trace(14605): error opening trace file: No such file or directory (2)
如果我改变我的设备,我想动态修改我的片段。如果我写这个我没有上次错误(我破坏savedInstanceState):
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
savedInstanceState.remove("android:support:fragments");
}
更新这是我的listFragment destroy ..
@Override
public void onDestroyView(){
super.onDestroyView();
// libera l'adapter e chiudo il thread
setListAdapter(null);
threadutenti.cancel(true);
}
没关系,但在我的应用程序中,我必须始终将savedInstanceState保存为空...我该怎么办?