Android: NPE in TabSpec setContent (View)

I am trying to set the contents of a tab in my TabHost as a RelativeLayout (defined at the top level of one of my XML files).

I tried to identify it with both R..layout.lobby_tab and R.id.lobby_base (the identifier is added as an element in the XML declaration).

In any case, 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 configure 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 (problem resolved)

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

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

This solved the problem. Thank you for your time!

+7
source share
1 answer

This error also occurs when you are not expanding your activity from TabActivity - in this case you need to manually call the setup() method on the TabHost instance.

+13
source

All Articles