2012-03-20 55 views
0

每当我锁定屏幕,返回,旋转屏幕或点击主屏幕时,我的应用程序崩溃。此应用程序使用3个选项卡实现片段UI。在我的清单中,我有android:configChanges =“orientation”,并且这个工作正常,直到我改变了一些东西(我不记得它是什么)。现在,即使在我的清单中,我的应用程序也无法处理旋转更改。我试图实现onSaveInstanceState,onRestoreInstanceState,onPause和onResume,但它继续崩溃。下面是一些代码:Android片段不保存状态,崩溃旋转/屏幕锁定/返回

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Inflate the layout 
    setContentView(R.layout.main); 

    // Initialize the TabHost 
    this.initializeTabHost(savedInstanceState); 
    if (savedInstanceState != null) { 
     // set the tab as per the saved state 
     mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
    } 
    // Initialize ViewPager 
    this.initializeViewPager(); 

} 

@Override 
protected void onPause() { 
    super.onPause();  
    } 

@Override 
protected void onResume() { 
super.onResume();   
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("tab", 
      mTabHost.getCurrentTabTag()); // save the tab selected 

} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    String myString = savedInstanceState.getString("tab"); 

} 

logcat的应用程序启动&崩溃

+0

你会得到什么错误信息?请检查日志检查应用程序,如LogCat。 – Raceimaztion 2012-03-20 02:18:22

+0

这里是logcat http://pastebin.com/dU4663Ey – user1275954 2012-03-20 02:40:20

回答

2
super.onSaveInstanceState(outState); 
outState.putString("tab", 
     mTabHost.getCurrentTabTag()); // save the tab selected 

应该

outState.putString("tab", 
     mTabHost.getCurrentTabTag()); // save the tab selected 

super.onSaveInstanceState(outState);

你需要 “标签” 添加到outState在调用onSaveInstanceState之前,为了避免出现exceptio n在OnCreate中加载时。换句话说,你的“标签”从来没有被实际保存过,因为你在添加“标签”到状态之前保存状态。

+0

即使我改变了,我仍然得到相同的异常。我的代码实际上是之前,我不知道为什么我改变它。可能只是为了试验。但仍然,我得到了/ /场描述符#12相同的问题I public static final int POP_BACK_STACK_INCLUSIVE = 1; – user1275954 2012-03-20 02:46:24

+0

你能发布你正在得到的确切错误吗? – lrAndroid 2012-03-20 02:51:41

+0

我不知道如何,我的意思是我可以找到错误,但是当应用程序崩溃时,在eclipse中打开“FragmentManager.class”,然后说“Source Not Found”。 这有帮助吗?:http://pastebin.com/MtrFFNBc – user1275954 2012-03-20 03:00:47