Android 2.1 NullPointerException with TabWidgets

后端 未结 4 1605
猫巷女王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 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.

提交回复
热议问题