gpt4 book ai didi

将 TabSpec 添加到 TabHost 时 Android 应用程序崩溃 - "Resource ID not valid"

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:32 25 4
gpt4 key购买 nike

我关注了 Android tutorial for TabLayout ,我现在正尝试通过使用布局以及每个选项卡的 Activity 来修改它。不幸的是,在将 TabSpec 添加到我的 TabHost 时,我的应用程序崩溃并出现“资源 ID 无效”错误。

我认为这里的错误与 Dalvik 不理解我对选项卡布局 XML 文件的引用有关,但不明白为什么会这样。我尝试使用额外的 <include>我的 main.xml 文件中的语句无济于事。非常欢迎任何帮助!

崩溃的 Java 代码:

public class TabLayoutActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources res = getResources();
//mTabHost = getTabHost();
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();

TabHost.TabSpec spec;
Intent intent;

//Search tab
intent = new Intent(this, SearchActivity.class);
spec = mTabHost.newTabSpec("search_tab");
spec.setIndicator("Search", res.getDrawable(R.drawable.magnifyingglass));
spec.setContent(intent);
mTabHost.addTab(spec); //this line crashes


//Allergies tab - when the above tab is commented out, this tab adds successfully
intent = new Intent(this, AllergiesActivity.class);
spec = mTabHost.newTabSpec("allergies_tab")
.setIndicator("Allergies", res.getDrawable(R.drawable.man))
.setContent(intent);

mTabHost.addTab(spec);

mTabHost.setCurrentTab(0);
}
}

违规标签的 Activity :

public class SearchActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.search_tab_layout);
}
}

另一个(工作)选项卡的 Activity (注意未指定布局):

public class AllergiesActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Allergies");
setContentView(textView);

}
}

主.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"
android:orientation="vertical">

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

</TabWidget>

<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


</FrameLayout>
</LinearLayout>

</TabHost>

search_tab.xml - 这是否没有以某种方式包含在内?

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


</LinearLayout>

日志输出:

04-09 16:08:23.434: D/ddm-heap(532): Got feature list request
04-09 16:08:23.843: D/AndroidRuntime(532): Shutting down VM
04-09 16:08:23.843: W/dalvikvm(532): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-09 16:08:23.843: E/AndroidRuntime(532): Uncaught handler: thread main exiting due to uncaught exception
04-09 16:08:23.873: E/AndroidRuntime(532): java.lang.RuntimeException: Unable to start activity ComponentInfo{mattjackson.allergyassassin/mattjackson.allergyassassin.TabLayoutActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{mattjackson.allergyassassin/mattjackson.allergyassassin.SearchActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050003 type #0x12 is not valid
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.os.Looper.loop(Looper.java:123)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-09 16:08:23.873: E/AndroidRuntime(532): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 16:08:23.873: E/AndroidRuntime(532): at java.lang.reflect.Method.invoke(Method.java:521)
04-09 16:08:23.873: E/AndroidRuntime(532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-09 16:08:23.873: E/AndroidRuntime(532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-09 16:08:23.873: E/AndroidRuntime(532): at dalvik.system.NativeStart.main(Native Method)
04-09 16:08:23.873: E/AndroidRuntime(532): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{mattjackson.allergyassassin/mattjackson.allergyassassin.SearchActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050003 type #0x12 is not valid
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.widget.TabHost.addTab(TabHost.java:213)
04-09 16:08:23.873: E/AndroidRuntime(532): at mattjackson.allergyassassin.TabLayoutActivity.onCreate(TabLayoutActivity.java:31)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-09 16:08:23.873: E/AndroidRuntime(532): ... 11 more
04-09 16:08:23.873: E/AndroidRuntime(532): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050003 type #0x12 is not valid
04-09 16:08:23.873: E/AndroidRuntime(532): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1870)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.content.res.Resources.getLayout(Resources.java:730)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-09 16:08:23.873: E/AndroidRuntime(532): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.Activity.setContentView(Activity.java:1622)
04-09 16:08:23.873: E/AndroidRuntime(532): at mattjackson.allergyassassin.SearchActivity.onCreate(SearchActivity.java:11)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 16:08:23.873: E/AndroidRuntime(532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-09 16:08:23.873: E/AndroidRuntime(532): ... 20 more
04-09 16:08:23.903: I/dalvikvm(532): threadid=7: reacting to signal 3
04-09 16:08:23.903: E/dalvikvm(532): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

最佳答案

在您的 SearchActivity 中,您必须设置布局。您尝试将 View (来自您的布局)设置为您的 Activity 布局。而不是

 setContentView(R.id.search_tab_layout);

 setContentView(R.layout.search_tab);

关于将 TabSpec 添加到 TabHost 时 Android 应用程序崩溃 - "Resource ID not valid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10075695/

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