Android 2.1 NullPointerException with TabWidgets

后端 未结 4 1519
猫巷女王i
猫巷女王i 2021-01-06 03:49

I have an issue I have not been able to figure out and it is only happening on devices running <2.1. It works fine on android 2.2. I have ansynchronous task that displa

相关标签:
4条回答
  • 2021-01-06 03:54

    An easy solution is to set the tabWidget visibility to gone in your layout

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:visibility="gone" />
    

    Then once you have defined your tabs content, you can make it visible again:

    // Add tab content (here a fragment class)
    tabHost.addTab(
        tabHost.newTabSpec("tag1").setIndicator("Title"), 
        contentFragment.class, 
        null);
    
    // Set tabWidget visible again
    tabWidget.setVisibility(View.VISIBLE);
    
    0 讨论(0)
  • 2021-01-06 04:03

    Had the same problem with tabs populated from AsyncTask on 1.6 and 2.1. Looks like earlier versions doesn't like TabHost without any tabs. To solve it, I do not use TabActivity and create TabHost with all its hierarchy manually in onPostExecute function of AsyncTask.

    0 讨论(0)
  • 2021-01-06 04:09
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
    

    Are you allowed to use the same 'tag' for more than one TabSpec? I'd try setting those correctly and see if it fixes it.

    EDIT: OK, so my suggestion didn't fix it but it makes sense to have unique tags anyway.

    Try this to see if it helps. Add an option to set the currently selected tab at the end of your LoadLayout() method like so (see last line)...

    /* Add tabSpec to the TabHost to display. */
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    
    tabHost.setCurrentTab(0); // <== Add this
    

    EDIT2: I found the TabWidget.java source and line 206 (where the NullPointerException occurs) is...

    mBottomLeftStrip.setState(selectedChild.getDrawableState());

    ...there are three possible causes of the exception that I can see.

    1. mBottonLeftStrip is null (highly unlikely)
    2. selectedCHild is null (TabWidget should default to child 0 and using tabHost.setCurrentTab() would have enforced that anyway)
    3. The result of selectedChild.getDrawableState() is null

    The last seems to be the likely cause but I'm not sure what could cause it to return null.

    Try Google for 'TabWidget.java source' - the second result points at grepcode.com which has line numbers and you can see what it's trying to do at the point of the exception.

    0 讨论(0)
  • 2021-01-06 04:16

    Inspired by one of related TabHost answers:

    public class FixedTabHost extends FragmentTabHost
    {
        public FixedTabHost(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas)
        {
            try { super.dispatchDraw(canvas); }
            catch (Exception ignored) {}
        }
    }
    

    Worked for me.

    0 讨论(0)
提交回复
热议问题