我正在尝试Tabs1.java
从 Android API Demos 16(并在 API 16 模拟器上运行)进行修改以使用标准Activity
而不是已弃用的TabActivity
类。我创建了一个新的布局文件并修改了代码。
我尝试使用这种方法而不是操作栏的原因是选项卡不适用于整个屏幕,仅适用于较小的子视图。
当我执行代码时,我在执行以下行时得到了一个NullPointerException
内部 Android方法:setContent
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