gpt4 book ai didi

android - 我的 Android TabHost 崩溃会出现 NullPointer 异常

转载 作者:行者123 更新时间:2023-12-02 06:58:50 26 4
gpt4 key购买 nike

嗨,我真的需要一些关于使用 android 的 tabhost 的帮助

问题是,当我单击将启动选项卡 Activity 的列表项时,它会崩溃,调试器或日志猫中没有错误。选项卡主机要控制 3 个或更多 Activity ,并且选项卡小部件嵌套在水平 ScrollView 中...我知道这不是 ScrollView 导致它崩溃,因为它在我添加它之前崩溃了。

这是我的选项卡 Activity 代码:

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)

最佳答案

Horizo​​ntalScrollView 只能有一个 subview ,您需要像这样更改 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>

关于android - 我的 Android TabHost 崩溃会出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32133746/

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