nick
nick

Reputation: 2853

Removing tabs from tabhost causes app to crash

I need to be able to close tabs in a TabHost for an android app I'm working on. I am currently calling this code.

tabHost.getTabWidget().removeViewAt(toDelete);

Where toDelete is the index of the tab that called the removal method. It looks like that code removes the tab, from the split second I can see it before it crashes. I'm thinking there's another removal method I need to call somewhere, because it appears to crash when attempting to draw the tab that was removed. I also tried

tabHost.removeViewAt(toDelete);

But it crashed because it didn't have a view at toDelete, which is expected since (I think) it just contains the tab widget rather than the actual tabs.

Here are my crash logs

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)

Any ideas on what other methods I should be calling? Or am I going down the entirely wrong path?

Edit: I just tried opening 5 tabs and closing the second one. It didn't crash at first, but it did have some VERY odd behavior. When I click on a tab, it shows the next one to the right as being highlighted, but displays the correct tab, until I click the last tab at which point the app crashes. A few screen shots in case it helps to describe my issue.

When I click the songs tab before closing a tab Displays songs in song tab

When I click the tab for a specific song before closing a tab Displays song lyrics in correct tab

When I click the songs tab after closing the current program tab. The correct contents are displayed but the wrong tab looks selected. After clicking the songs tab after closing current program tab

Upvotes: 1

Views: 626

Answers (2)

Andrey Degtiarev
Andrey Degtiarev

Reputation: 87

Before calling tabs.getTabWidget().removeView, you need show another tab, tabs.setCurrentTab(0);

Upvotes: 2

Nyle
Nyle

Reputation: 89

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

I remove it by this method , you can try it with the method

Upvotes: 0

Related Questions