gpt4 book ai didi

java - getTabHost() 返回 null

转载 作者:行者123 更新时间:2023-11-29 22:25:01 24 4
gpt4 key购买 nike

我正在按照 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>

可绘制状态列表

<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>

我什至尝试将可绘制状态列表中的图像从/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)

还有其他人在实现标准选项卡 Activity 时遇到过问题吗?关于我做错了什么的任何指示?

最佳答案

尝试将 buildTabs() 的所有代码放入您的 onCreate() 方法中。

关于java - getTabHost() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com