问题:

我正在尝试创建两个包含FragmentsFragmentTabHost的选项卡。

这是将选项卡添加到FragmentTabHost的位置(包含选项卡的onCreateFragmentActivity方法:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_financials);

    mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec(TAG_ONE).setIndicator("Tab #1",
                    getResources().getDrawable(android.R.drawable.star_on)),
                    MyFragmentOne.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec(TAG_TWO).setIndicator("Tab #2",
                    getResources().getDrawable(android.R.drawable.star_on)),
                    MyFragmentTwo.class, null);


    if(savedInstanceState == null) {
        new MyAsyncTask(MyClass.this).execute();
    }
    else {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("TAB_TAG"));
    }

}


但是,问题是MyFragmentOneMyFragmentTwo都在等待MyAsyncTask中的数据。也就是说,只有MyAsyncTask完成运行后,才能正确创建每个片段中的视图。

因此,添加标签时最终得到了NullPointerException,因为AsyncTask尚未提取所需的数据。

我的解决方案尝试:

我尝试在TabHost#addTab()onPostExecute()中调用MyAsyncTask,以便仅在MyAsyncTask完成后才创建片段,从而避免了任何空指针。

但是,通过将addTab()调用移动到onPostExecute()AsyncTask,我得到了错误:No tab tag for null。下面的Logcat。

Logcat:

08-06 06:15:17.947: E/AndroidRuntime(1968): java.lang.IllegalStateException: No tab known for tag null
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:330)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:280)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.View.dispatchAttachedToWindow(View.java:12585)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2458)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2465)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1217)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.os.Handler.handleCallback(Handler.java:733)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.os.Looper.loop(Looper.java:136)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at java.lang.reflect.Method.invoke(Method.java:515)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 06:15:17.947: E/AndroidRuntime(1968):     at dalvik.system.NativeStart.main(Native Method)


那么我该如何解决这个问题呢?

最佳答案

我最终使用了空白的PlaceHolderFragment并以编程方式在onPostExecute()中将其删除,如下所示:

mTabHost.addTab(
            mTabHost.newTabSpec("Placeholder one").setIndicator("Tab #1",
                    getResources().getDrawable(android.R.drawable.star_on)),
                    PlaceHolderFragment.class, null);
mTabHost.addTab(
            mTabHost.newTabSpec("Placeholder two").setIndicator("Tab #2",
                    getResources().getDrawable(android.R.drawable.star_on)),
                    PlaceHolderFragment.class, null);


然后在onPostExecute()中:

mTabHost.setCurrentTab(0);
mTabHost.clearAllTabs();

mTabHost.addTab(
        mTabHost.newTabSpec(TAG_ONE).setIndicator("Tab #1",
                getResources().getDrawable(android.R.drawable.star_on)),
                MyFragmentOne.class, null);
mTabHost.addTab(
        mTabHost.newTabSpec(TAG_TWO).setIndicator("Tab #2",
                getResources().getDrawable(android.R.drawable.star_on)),
                MyFragmentTwo.class, null);

07-28 13:40