gpt4 book ai didi

android - FragmentTabHost 和异步任务

转载 作者:行者123 更新时间:2023-11-30 02:45:15 26 4
gpt4 key购买 nike

问题:

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

我在此处将选项卡添加到 FragmentTabHost(包含选项卡的 FragmentActivityonCreate 方法:

@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 运行完成后,才能正确创建每个 fragment 中的 View 。

因此,当我添加选项卡时,我最终得到了一个 NullPointerException,因为 AsyncTask 尚未获取所需的数据。

我的解决方案尝试:

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

但是,通过将 addTab() 调用移动到 AsyncTaskonPostExecute(),我得到错误:No null 的制表符标记。下面的 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);

关于android - FragmentTabHost 和异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com