我的活动中有大约 14 个选项卡,因此我习惯于HorizontalScrollable
使其可滚动。但是它给出了NullPointerException
错误。我不知道为什么。
XML:
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab5"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab6"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab7"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
这是代码:
public class Events2 extends Activity {
private TabHost thEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events2);
thEvent = (TabHost) findViewById(R.id.tabhost);
thEvent.setup();
// Tab 1
TabSpec specs = thEvent.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("PETROGYAN");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("PETROTALK");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("PETRO CASE STUDY");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag4");
specs.setContent(R.id.tab4);
specs.setIndicator("PETRODRAFT");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag5");
specs.setContent(R.id.tab5);
specs.setIndicator("PETROSELL");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag6");
specs.setContent(R.id.tab6);
specs.setIndicator("PETRO-DEBATE");
thEvent.addTab(specs);
specs = thEvent.newTabSpec("tag7");
specs.setContent(R.id.tab7);
specs.setIndicator("IDP");
thEvent.addTab(specs);
}
}
PS我已经缩短了发布这个的代码。但我在原始活动中使用了 14 个标签
日志猫:
09-20 19:36:35.461: E/AndroidRuntime(2796): FATAL EXCEPTION: main
09-20 19:36:35.461: E/AndroidRuntime(2796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rakeshsarangi.petrofiesta2013/com.rakeshsarangi.petrofiesta2013.Events2}: java.lang.NullPointerException
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.os.Looper.loop(Looper.java:137)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-20 19:36:35.461: E/AndroidRuntime(2796): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 19:36:35.461: E/AndroidRuntime(2796): at java.lang.reflect.Method.invoke(Method.java:511)
09-20 19:36:35.461: E/AndroidRuntime(2796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 19:36:35.461: E/AndroidRuntime(2796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 19:36:35.461: E/AndroidRuntime(2796): at dalvik.system.NativeStart.main(Native Method)
09-20 19:36:35.461: E/AndroidRuntime(2796): Caused by: java.lang.NullPointerException
09-20 19:36:35.461: E/AndroidRuntime(2796): at com.rakeshsarangi.petrofiesta2013.Events2.onCreate(Events2.java:22)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.Activity.performCreate(Activity.java:4465)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 19:36:35.461: E/AndroidRuntime(2796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-20 19:36:35.461: E/AndroidRuntime(2796): ... 11 more