我的Activity中有大约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);
}
}
P.S。我已经缩短了发布此代码的代码。但我在原始活动中使用了14个标签
logcat的:
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
答案 0 :(得分:1)
// try this
1. when u define id xml this way android:id="@android:id/tabhost" then u get this in activity like findViewById(android.R.id.tabhost);
2. when u define id xml this way android:id="@+id/tab1" then u get this in activity like findViewById(R.id.tab1);
// in ur case try this
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_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>
</FrameLayout>
</LinearLayout>
</TabHost>
private TabHost thEvent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thEvent = (TabHost) findViewById(android.R.id.tabhost);
thEvent.setup();
// Tab 1
TabHost.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);
}
答案 1 :(得分:0)
试试这个..
只需替换下面的行
thEvent = (TabHost) findViewById(R.id.tabhost);
thEvent.setup();
作为
thEvent = getTabHost();
或
thEvent = (TabHost)findViewById(android.R.id.tabhost);