[英]My Android TabHost Is Crashing Will NullPointer Exception
嗨,我真的需要使用Android的Tabhost幫助
問題是,當我單擊將啟動選項卡活動的列表項時,它崩潰,調試器或日志貓中沒有錯誤。 選項卡宿主要控制3個或更多活動,並且選項卡小部件嵌套在水平滾動視圖中……我知道不是滾動視圖導致它崩潰,因為它在我添加之前崩潰了。
這是我的標簽活動代碼:
public class BulbaTab extends TabActivity {
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tablay);
// create the TabHost that will contain the Tabs
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("Dex Entry");
tab1.setIndicator("Dex Entry");
tab1.setContent(new Intent(this,Bulbasaur.class));
TabHost.TabSpec tab2 = tabHost.newTabSpec("Moves List");
tab2.setIndicator("Moves");
tab2.setContent(new Intent(this,BulbasaurMovesList.class));
TabHost.TabSpec tab3 = tabHost.newTabSpec("Evolution & Breeding");
tab3.setIndicator("Evolution & Breeding");
tab3.setContent(new Intent(this,BulbasaurBreeding.class));
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
}
}
這是tabhost的布局:
TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/horizontalScrollView"
TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
TabWidget
HorizontalScrollView
FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
FrameLayout
LinearLayout
TabHost
必須刪除不會在網站上顯示的標簽
這是顯示的錯誤
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.BulbaTab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.Bulbasaur}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{net.metaruptdonations.MetaDex/net.metaruptdonations.MetaDex.Bulbasaur}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2140)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost.addTab(TabHost.java:240)
at net.metaruptdonations.MetaDex.BulbaTab.onCreate(BulbaTab.java:39)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at net.metaruptdonations.MetaDex.Bulbasaur.onCreate(Bulbasaur.java:48)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2140)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost.addTab(TabHost.java:240)
at net.metaruptdonations.MetaDex.BulbaTab.onCreate(BulbaTab.java:39)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
HorizontalScrollView只能有一個子視圖,您需要像這樣更改xml
<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:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
</FrameLayout>
</LinearLayout>
</TabHost>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.