gpt4 book ai didi

java - TabHost 未正确加载?

转载 作者:行者123 更新时间:2023-12-01 13:46:27 26 4
gpt4 key购买 nike

我的 TabHost Activity 由于错误而无法正确加载。这是我的代码、xml 和错误日志:

public class TabScreen extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_screen);

TabHost t = getTabHost();
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);


//For New Photos
TabSpec newSpec = tabHost.newTabSpec("NewPhoto");
newSpec.setIndicator("NewPhoto", getResources().getDrawable(R.drawable.ic_tab_newphoto));
Intent phNewIntent = new Intent(this, NewPhoto.class);
newSpec.setContent(phNewIntent);
//For List of Taken Photos
TabSpec listSpec = tabHost.newTabSpec("PhotoList");
listSpec.setIndicator("PhotoList", getResources().getDrawable(R.drawable.ic_tab_photolist));
Intent phListIntent = new Intent(this, PhotoList.class);
listSpec.setContent(phListIntent);
//For Map
TabSpec mapSpec = tabHost.newTabSpec("PhotoMap");
mapSpec.setIndicator("PhotoMap", getResources().getDrawable(R.drawable.ic_tab_photomap));
Intent phMapIntent = new Intent(this, PhotoMap.class);
mapSpec.setContent(phMapIntent);

tabHost.addTab(newSpec);
tabHost.addTab(listSpec);
tabHost.addTab(mapSpec);
}


}

还有我的 XML:

<?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="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<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"/>

</LinearLayout>

最后,我的错误消息:(有一个很长的列表,但我认为问题的主要是我正在粘贴的内容)

12-02 16:00:06.250: E/AndroidRuntime(3757): FATAL EXCEPTION: main
12-02 16:00:06.250: E/AndroidRuntime(3757): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.TabScreen}: java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.NewPhoto}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

该网站上有多个关于此问题的条目,但在查看每个条目后,没有一个解决方案有效。我的 xml 具有正确的 @android:id/tabhost,我删除了 R.java 文件并多次清理了整个项目。如果这有帮助,当我进行一些调试时,应用程序崩溃的地方是 tabHost.addTab(newSpec); 。有人知道这是怎么回事吗?

编辑:完整错误日志:

12-02 17:02:43.296: E/AndroidRuntime(8121): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.TabScreen}:     java.lang.RuntimeException: Your content must have a TabHost whose id attribute is     'android.R.id.tabhost'
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.os.Looper.loop(Looper.java:130)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-02 17:02:43.296: E/AndroidRuntime(8121): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 17:02:43.296: E/AndroidRuntime(8121): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-02 17:02:43.296: E/AndroidRuntime(8121): at dalvik.system.NativeStart.main(Native Method)
12-02 17:02:43.296: E/AndroidRuntime(8121): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.Activity.setContentView(Activity.java:1657)
12-02 17:02:43.296: E/AndroidRuntime(8121): at edu.wcu.snipsnaps.TabScreen.onCreate(TabScreen.java:16)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-02 17:02:43.296: E/AndroidRuntime(8121): ... 11 more

最佳答案

在代码中尝试一下

TabHost tabHost = (TabHost)findViewById(R.id.tabhost);

而不是:

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

在 XML 中:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

而不是

<?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="fill_parent"
android:layout_height="fill_parent">

还有可能您需要将 View 中的所有 id 从 android:id="@android:id...."更改为 android:id="@+id/.....", @android 为 android 默认保留元素

帮助链接http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

关于java - TabHost 未正确加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337998/

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