我已经遵循了TabLayout 的 Android 教程,现在我正在尝试通过使用布局以及每个选项卡的活动来修改它。不幸的是,将 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);
}
}
违规标签的活动:
public class SearchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.search_tab_layout);
}
}
另一个(工作)选项卡的活动(注意未指定布局):
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