7

I'm trying to set the content of a tab in my TabHost to be a RelativeLayout (defined in the top-level of one of my XML files).

I've tried identifying it both by using R.layout.lobby_tab and R.id.lobby_base (the ID is added as an item in XML declaration).

Either way, I get a NullPointerException in the setContent(View) method:

02-12 22:42:12.907: E/AndroidRuntime(2028): FATAL EXCEPTION: main
{webs.winbatch.dwd/webs.winbatch.dwd.HostActivity}: java.lang.NullPointerException
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.os.Looper.loop(Looper.java:123)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at java.lang.reflect.Method.invoke(Method.java:507)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at dalvik.system.NativeStart.main(Native Method)
02-12 22:42:12.907: E/AndroidRuntime(2028): Caused by: java.lang.NullPointerException
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:589)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:584)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.widget.TabHost$TabSpec.setContent(TabHost.java:441)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at webs.winbatch.dwd.HostActivity.setUpTabs(HostActivity.java:20)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at webs.winbatch.dwd.HostActivity.onCreate(HostActivity.java:13)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 22:42:12.907: E/AndroidRuntime(2028):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 22:42:12.907: E/AndroidRuntime(2028):     ... 11 more

Here is the code that runs to set up the tabs:

    TabHost thost = (TabHost) findViewById(android.R.id.tabhost);
    TabHost.TabSpec spec;

    if(thost !=null) {
        spec = thost.newTabSpec("lobby").setIndicator("Lobby").setContent(R.id.lobby_base);
        thost.addTab(spec);
    }

UPDATE (Issue Resolved)

I needed to add this line to the FrameLayout in the XML file:

<include layout="@layout/lobby_tab"/>

This resolved the issue. Thanks for your time!

bgroenks
  • 1,859
  • 5
  • 34
  • 63
  • Are you sure `R.id.lobby_base` is a valid view layout? Should it perhaps be `R.layout.lobby_base`? – Squonk Feb 14 '12 at 23:01
  • @MisterSquonk I also tried its R.layout value which is R.layout.lobby_tab. NPE still occurred. – bgroenks Feb 14 '12 at 23:16
  • Sorry, I hadn't spotted that bit in your question. Which line is throwing the exception? The one where you create the `newTabSpec` or when you `addTab`? – Squonk Feb 14 '12 at 23:26
  • newTabSpec line. The setContent(...) method specifically. – bgroenks Feb 15 '12 at 02:03
  • If you're using Eclipse then use Project -> Clean which will wipe the auto-generated R.java file and regenerate it. It could be it has become out of sync depending on any changes you may have made recently. – Squonk Feb 15 '12 at 02:22
  • Cleaning appeared to have no effect on the issue. – bgroenks Feb 15 '12 at 03:53
  • If it's worth noting, I tried logging the value of R.id.lobby_base and it does return a valid value. – bgroenks Feb 15 '12 at 03:55
  • are you using Tabactivity? check if thost is not null(use toString()) – Its not blank Feb 15 '12 at 03:58
  • or you can use an Intent to setCOntent.... – Its not blank Feb 15 '12 at 04:03
  • I am not using TabActivity. TabHost is not null. I do not feel that an Intent is very well suited to my situation, I would much rather use Views. I tried calling clearAllTabs() prior to calling setup() and the same NPE was thrown on the call to clearAllTabs(). I don't understand why this is happening... it's like the TabHost is completely not valid but why would that be? – bgroenks Feb 15 '12 at 04:07
  • Check OP for update. I resolved the issue. – bgroenks Feb 15 '12 at 04:19

1 Answers1

13

This bug also manifests when you do not extend your activity from TabActivity - in this case you need to manually call the setup() method on the TabHost instance.

Martin Vysny
  • 3,088
  • 28
  • 39