从tabhost中删除选项卡会导致应用程序崩溃

时间:2012-04-04 16:33:50

标签: java android android-tabhost

我需要能够在TabHost中关闭我正在处理的Android应用程序的标签。我目前正在调用此代码。

tabHost.getTabWidget().removeViewAt(toDelete);

where toDelete是调用remove方法的选项卡的索引。它看起来像代码删除了标签,从崩溃之前我可以看到它。我想有一个我需要在某个地方调用的删除方法,因为它在尝试绘制已删除的选项卡时似乎崩溃了。我也试过

tabHost.removeViewAt(toDelete);

但它崩溃了,因为它没有toDelete的视图,这是预期的,因为(我认为)它只包含tab小部件而不是实际的标签。

以下是我的崩溃日志

04-04 16:05:53.149: E/AndroidRuntime(7885): FATAL EXCEPTION: main
04-04 16:05:53.149: E/AndroidRuntime(7885): java.lang.NullPointerException
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.widget.TabWidget.dispatchDraw(TabWidget.java:323)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.View.draw(View.java:10981)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.widget.FrameLayout.draw(FrameLayout.java:450)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.os.Looper.loop(Looper.java:137)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-04 16:05:53.149: E/AndroidRuntime(7885):     at dalvik.system.NativeStart.main(Native Method)

关于我应该调用的其他方法的任何想法?还是我走错了路?

编辑:我刚尝试打开5个标签并关闭第二个标签。它起初没有崩溃,但确实有一些非常奇怪的行为。当我单击选项卡时,它会显示右侧的下一个突出显示,但显示正确的选项卡,直到我单击应用程序崩溃的最后一个选项卡。一些屏幕截图,以防有助于描述我的问题。

在关闭标签之前单击歌曲标签 Displays songs in song tab

在关闭标签之前单击特定歌曲的标签 Displays song lyrics in correct tab

关闭当前程序选项卡后单击歌曲选项卡。显示正确的内容但看起来选中了错误的选项卡。 After clicking the songs tab after closing current program tab

2 个答案:

答案 0 :(得分:2)

在致电tabs.getTabWidget().removeView之前,您需要显示另一个标签tabs.setCurrentTab(0);

答案 1 :(得分:0)

tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(index));

我通过此方法删除它,您可以尝试使用方法