gpt4 book ai didi

android - 布局中定义的 TabHost 在 setContent 期间给出 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:09 25 4
gpt4 key购买 nike

我正在尝试修改 Android API 演示 16 中的 Tabs1.java(并在 API 16 模拟器上运行)以使用标准 Activity 而不是已弃用的TabActivity类(class)。我创建了一个新的布局文件并修改了代码。

我尝试使用这种方法而不是操作栏的原因是选项卡不适用于整个屏幕,仅适用于较小的 subview 。

当我执行代码时,在执行以下行时,我在 Android setContent 方法中得到了一个NullPointerException:

tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.view1)

知道为什么会发生这种情况吗?完整的堆栈跟踪在代码和布局之后。

Tabs1VanillaActivity.java:

public class Tabs1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs1);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);

tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(R.id.view3));
}
}

tabs1.xml:

    <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView goes here!"
tools:ignore="HardcodedText" />

<TabHost
android:id="@+id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue"
android:text="@string/tabs_1_tab_1" />

<TextView
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/red"
android:text="@string/tabs_1_tab_2" />

<TextView
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/green"
android:text="@string/tabs_1_tab_3" />
</FrameLayout>
</LinearLayout>
</TabHost>

</LinearLayout>

堆栈跟踪:

09-18 16:18:22.968: E/AndroidRuntime(1191): FATAL EXCEPTION: main
09-18 16:18:22.968: E/AndroidRuntime(1191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.apis/com.example.android.apis.view.Tabs1}: java.lang.NullPointerException
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.os.Looper.loop(Looper.java:137)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-18 16:18:22.968: E/AndroidRuntime(1191): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 16:18:22.968: E/AndroidRuntime(1191): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 16:18:22.968: E/AndroidRuntime(1191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 16:18:22.968: E/AndroidRuntime(1191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 16:18:22.968: E/AndroidRuntime(1191): at dalvik.system.NativeStart.main(Native Method)
09-18 16:18:22.968: E/AndroidRuntime(1191): Caused by: java.lang.NullPointerException
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:617)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:612)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.widget.TabHost$TabSpec.setContent(TabHost.java:461)
09-18 16:18:22.968: E/AndroidRuntime(1191): at com.example.android.apis.view.Tabs1.onCreate(Tabs1.java:42)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.Activity.performCreate(Activity.java:4465)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-18 16:18:22.968: E/AndroidRuntime(1191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-18 16:18:22.968: E/AndroidRuntime(1191): ... 11 more

最佳答案

在尝试添加选项卡之前,您必须调用 tabHost.setup()

http://developer.android.com/reference/android/widget/TabHost.html#setup()

关于android - 布局中定义的 TabHost 在 setContent 期间给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481045/

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