getTabHost() is returning null
I am following the directions for a TabLayout. Everything is pretty much an exact copy from that page.
public class DashboardActivity extends TabActivity {
the problem is that in this line TabHost tabHost = getTabHost();
, tabHost is always null, which causes a null pointer exception in the TabActivity
base class.
Is there something I am missing?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
profile = Controller.getProfile();
this.setContentView(R.layout.test);
buildTabs();
}
private void buildTabs()
{
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();// The activity TabHost
if(tabHost != null)
{
// Initialize a TabSpec for each tab and add it to the TabHost
TabSpec statementSpec = tabHost.newTabSpec("statements").setIndicator("Statements (" + profile.Statements.length + ")",
res.getDrawable(R.drawable.ic_tabs_statements))
.setContent(new Intent().setClass(DashboardActivity.this, SplashActivity.class));
tabHost.addTab(statementSpec);
TabSpec paymentSpec = tabHost.newTabSpec("payments").setIndicator("Payments (" + profile.Items.length + ")",
res.getDrawable(R.drawable.ic_tabs_payments))
.setContent(new Intent().setClass(DashboardActivity.this, SplashActivity.class));
tabHost.addTab(paymentSpec);
tabHost.setCurrentTab(0);
}
}
Layout
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:lay开发者_JAVA百科out_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
state-list drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/ic_tab_active"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_suspended" />
</selector>
I even tried moving the images in the state-list drawable from /rex/drawable to /res/drawable-hdpi. I always get the same error.
05-31 21:22:23.753: ERROR/AndroidRuntime(647): FATAL EXCEPTION: main
05-31 21:22:23.753: ERROR/AndroidRuntime(647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ftni.consumer/com.ftni.consumer.ui.DashboardActivity}: java.lang.NullPointerException
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.os.Looper.loop(Looper.java:123)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread.main(ActivityThread.java:3647)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at java.lang.reflect.Method.invoke(Method.java:507)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at dalvik.system.NativeStart.main(Native Method)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): Caused by: java.lang.NullPointerException
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.TabActivity.onPostCreate(TabActivity.java:79)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1108)
05-31 21:22:23.753: ERROR/AndroidRuntime(647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1605)
Has anyone else had problems implementing the standard tab activity? Any pointers on what I am doing wrong?
Try putting all of the code for buildTabs() in your onCreate() method.
Write
tabHost.setup();
instead of:
tabHost = getTabHost();
This should solve the problem.
精彩评论