更改为横向时,Android标签片段崩溃

时间:2014-03-10 20:29:21

标签: android tabs orientation fragment landscape

我有一个使用片段的3个标签的活动,它在纵向模式下工作时工作正常,但是,当我将其更改为横向时,它会崩溃。这是代码:

public class MainMenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        String label1 = getResources().getString(R.string.userMainTab);
        Tab tab = actionBar.newTab();
        tab.setText(label1);
        TabListener<UserTabActivity> tl = new TabListener<UserTabActivity>(this, label1, UserTabActivity.class);
        tab.setTabListener(tl);
        actionBar.addTab(tab);

        String label2 = getResources().getString(R.string.credentialGroupMainTab);
        Tab tab2 = actionBar.newTab();
        tab2.setText(label2);
        TabListener<CredentialGroupActivity> tl2 = new TabListener<CredentialGroupActivity>(this, label2, CredentialGroupActivity.class);
        tab2.setTabListener(tl2);
        actionBar.addTab(tab2);

        String label3 = getResources().getString(R.string.credentialTabHeader);
        Tab tab3 = actionBar.newTab();
        tab3.setText(label3);
        TabListener<CredentialTabActivity> tl3 = new TabListener<CredentialTabActivity>(this, label2, CredentialTabActivity.class);
        tab3.setTabListener(tl3);
        actionBar.addTab(tab3);     

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }   

    public static class TabListener<T extends Fragment> implements ActionBar.TabListener{

        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        public TabListener(Activity activity, String tag, Class<T> clz){
            mActivity = activity;
            mTag = tag;
            mClass = clz;

            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if(mFragment != null && !mFragment.isDetached()){
                FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
        }

        @Override
        public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
            if(mFragment == null){
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                arg1.add(android.R.id.content, mFragment, mTag);
            } else{
                arg1.attach(mFragment);
            }
        }

        @Override
        public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
            // TODO Auto-generated method stub
            if(mFragment != null){
                arg1.detach(mFragment);
            }
        }

    }

}

你能告诉我为什么不工作吗?错误是:

java.lang.RuntimeException: Unable to destroy activity{ACTIVITY_NAME} : java.lang.NullPointerException

我注意到它失败了,直到我点击任何标签,然后尝试将其更改为横向,但我无法在代码中识别发生了什么,而且,当我更改视图时,它丢失了它所在的标签。

先谢谢!

修改

为了在更改方向后保留选定的标签,由于纳撒尼尔瓦格纳 karthikk 提出的建议,我最终做到了这一点:

protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    outState.putInt("tabState", actionBar.getSelectedTab().getPosition());
}

//In the onCreate method
    if(savedInstanceState != null ){
        actionBar.selectTab(actionBar.getTabAt(savedInstanceState.getInt("tabState")));
    }

谢谢!

2 个答案:

答案 0 :(得分:3)

要保留标签选择,

  1. 必须优先存储或
  2. 覆盖onSaveInstanceState()并保存TAB_SELECTION。在配置更改时,您的活动将通过onRestoreInstanceState()API将先前状态作为Bundle。
  3. 我建议您参考http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)了解更多信息。

答案 1 :(得分:1)

你是否在你的片段中覆盖了onDestroy?您显示的NPE是我在Tabs中使用的片段中覆盖OnDestroy时出现的症状。如果你是的话,我会删除那个覆盖,看看是否能解决这个问题。