我正在按照TabLayout的说明进行操作。一切都是该页面的精确副本。
public class DashboardActivity extends TabActivity {
问题是在这一行TabHost tabHost = getTabHost();
中,tabHost始终为null,这会导致TabActivity
基类中的空指针异常。
我有什么遗失的吗?
@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);
}
}
布局
<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:layout_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>
我甚至尝试将状态列表drawable中的图像从/ rex / drawable移动到/ res / drawable-hdpi。我总是得到同样的错误。
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)
是否有其他人在实施标准制表符活动时遇到问题?关于我做错了什么的指示?
答案 0 :(得分:2)
尝试将buildTabs()的所有代码放在onCreate()方法中。
答案 1 :(得分:0)
写
tabHost.setup();
而不是:
tabHost = getTabHost();
这应该可以解决问题。