Android TabbedActivity(FragmentActvity,TabHost)onTabChanged抛出IllegalStateException:没有主机,无法重现

时间:2016-05-18 08:30:03

标签: android android-fragments android-fragmentactivity illegalstateexception android-tabbed-activity

我已经在我们的Android应用程序的新更新中修复了许多旧错误和崩溃,并且它看起来很好但是现在在更改生产版本的“MainActivity”选项卡时会记录新的崩溃。我尝试通过旋转,切换,关闭/打开和旋转等屏幕在多个不同的手机(Nexus 5,Nexus 4,小米Mi4C,三星Galaxy S5,HTC One A9和A8)上重现此崩溃,我只是无法'重现它:(。

崩溃发生在不同的手机型号和不同的Android版本(4.4.2至6.0.1)

崩溃:

java.lang.IllegalStateException: No host
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1194)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1207)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.attachFragment(SourceFile:1385)
at android.support.v4.app.BackStackRecord.run(SourceFile:728)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(SourceFile:545)
at myPackageName.FragmentTabsActivity$TabManager.onTabChanged(SourceFile:183)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:468)
at android.widget.TabHost.setCurrentTab(TabHost.java:447)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:170)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:550)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

FragmentTabsActivity清单条目:

    <activity
        android:name="myPackageName.FragmentTabsActivity"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:launchMode="singleTask" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

FragmentTabsActivity onTabChanged实现:

    public static class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final Map<String, TabInfo> mTabs= new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
        private final String mTag;
        private final Class<?> mClss;
        private final Bundle mArgs;
        private Fragment mFragment;

        TabInfo(String tag, Class<?> clazz, Bundle args) {
            mTag= tag;
            mClss= clazz;
            mArgs= args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

        public DummyTabFactory(Context context) {
            mContext= context;
        }

        @Override
        public View createTabContent(String tag) {
            View v= new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
        mActivity= activity;
        mTabHost= tabHost;
        mContainerId= containerId;
        mTabHost.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag= tabSpec.getTag();

        TabInfo info= new TabInfo(tag, clss, args);

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        info.mFragment= mActivity.getSupportFragmentManager().findFragmentByTag(tag);
        if (info.mFragment != null && !info.mFragment.isDetached()) {
            FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
            ft.detach(info.mFragment);
            ft.commitAllowingStateLoss();
        }

        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab= mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.mFragment != null) {
                    ft.detach(mLastTab.mFragment);
                }
            }
            if (newTab != null) {
                if (newTab.mFragment == null) {
                    newTab.mFragment= Fragment.instantiate(mActivity, newTab.mClss.getName(), newTab.mArgs);
                    ft.add(mContainerId, newTab.mFragment, newTab.mTag);
                } else {
                    ft.attach(newTab.mFragment);
                }
            }

            if (newTab.mFragment != null && newTab.mFragment instanceof MyMapFragment) {
                MyMapFragment mapFragment= (MyMapFragment) newTab.mFragment;
                mapFragment.clearBundleOnTabChange();
            }
            if (mLastTab != null && mLastTab.mFragment != null && mLastTab.mFragment instanceof MyMapFragment) {
                MyMapFragment mapFragment= (MyMapFragment) mLastTab.mFragment;
                mapFragment.clearBundleOnTabChange();
            }

            mLastTab= newTab;
            ft.commitAllowingStateLoss();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }
}

FragmentTabsActivity实际上包含3个选项卡,其中每个选项卡都是片段(还有什么:)?)。

如果有人知道如何重现/解决这个问题,我会很高兴答案。谷歌搜索时我已经找到了2或3种可能的解决方案,但是无法测试哪一种解决了我的问题我觉得不舒服:/

0 个答案:

没有答案