我创建了一个带有1个选项卡的TabHost示例,该选项运行正常。我使用 TabSpec.setContent(TabHost.TabContentFactory contentFactory)
在 TabContentFactory 的标签中添加视图但是,只要我尝试使用查看/资源ID 使用 TabSpec.setContent(int viewId)在标签中添加标签,我的应用就会崩溃。代码如下:
public class MainAct extends TabActivity implements TabContentFactory{
protected Resources res;
protected ListView listView;
protected LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
/*init*/
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
listView = new ListView(this);
/*prepare tabs*/
TabHost tabHost = getTabHost();
TabSpec spec;
spec = tabHost.newTabSpec(res.getString(R.string.label_project));
spec.setIndicator(res.getString(R.string.label_project));
spec.setContent(this); // << this works perfect
//spec.setContent(R.id.list_item); // << CRASHES
//spec.setContent(R.id.list_item2); // << CRASHES
tabHost.addTab(spec);
}
@Override
public View createTabContent(String tag) {
return listView;
}
}
我的XML文件如下
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="22sp"
android:id="@+id/list_item">
</TextView>
和
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="list_item2">
</TextView>
</LinearLayout>
它们都正确定义了ID,但它们崩溃了。我已经完成了以下在线教程:
http://android-pro.blogspot.com/2010/08/tabbed-applications-in-android.html
http://www.codeproject.com/KB/android/AndroidTabs.aspx
http://www.androidpeople.com/android-custom-listview-tutorial-example/
所有这些都描述了如何使用ID在标签中添加视图,但没有一个适用于我。
我想要做的就是在布局文件中定义一个视图,并使用ID将其添加到我的选项卡中。请帮忙。
编辑LogCat:
01-29 21:53:44.665: DEBUG/AndroidRuntime(1336): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-29 21:53:44.675: DEBUG/AndroidRuntime(1336): CheckJNI is ON
01-29 21:53:44.945: DEBUG/AndroidRuntime(1336): --- registering native functions ---
01-29 21:53:45.395: DEBUG/ddm-heap(1336): Got feature list request
01-29 21:53:45.934: DEBUG/PackageParser(63): Scanning package: /data/app/vmdl69673.tmp
01-29 21:53:45.944: INFO/PackageParser(63): org.pixeroid.gtd: compat added android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE
01-29 21:53:46.164: DEBUG/dalvikvm(63): GC freed 8169 objects / 609984 bytes in 156ms
01-29 21:53:46.285: INFO/PackageManager(63): Removing non-system package:org.pixeroid.gtd
01-29 21:53:46.285: DEBUG/PackageManager(63): Removing package org.pixeroid.gtd
01-29 21:53:46.295: DEBUG/PackageManager(63): Activities: org.pixeroid.gtd.MainAct
01-29 21:53:46.414: DEBUG/PackageManager(63): Scanning package org.pixeroid.gtd
01-29 21:53:46.414: INFO/PackageManager(63): /data/app/vmdl69673.tmp changed; unpacking
01-29 21:53:46.426: DEBUG/installd(31): DexInv: --- BEGIN '/data/app/vmdl69673.tmp' ---
01-29 21:53:46.735: DEBUG/dalvikvm(1342): DexOpt: load 53ms, verify 85ms, opt 3ms
01-29 21:53:46.745: DEBUG/installd(31): DexInv: --- END '/data/app/vmdl69673.tmp' (success) ---
01-29 21:53:46.755: DEBUG/PackageManager(63): Activities: org.pixeroid.gtd.MainAct
01-29 21:53:46.764: DEBUG/ActivityManager(63): Uninstalling process org.pixeroid.gtd
01-29 21:53:46.774: DEBUG/ActivityManager(63): Force removing process ProcessRecord{43f230b8 1327:org.pixeroid.gtd/10029} (org.pixeroid.gtd/10029)
01-29 21:53:46.774: INFO/Process(63): Sending signal. PID: 1327 SIG: 9
01-29 21:53:46.805: DEBUG/ActivityManager(63): Received spurious death notification for thread android.os.BinderProxy@43d66520
01-29 21:53:46.925: INFO/installd(31): move /data/dalvik-cache/data@app@vmdl69673.tmp@classes.dex -> /data/dalvik-cache/data@app@org.pixeroid.gtd.apk@classes.dex
01-29 21:53:46.934: DEBUG/PackageManager(63): New package installed in /data/app/org.pixeroid.gtd.apk
01-29 21:53:47.085: DEBUG/AndroidRuntime(1336): Shutting down VM
01-29 21:53:47.095: DEBUG/dalvikvm(1336): DestroyJavaVM waiting for non-daemon threads to exit
01-29 21:53:47.105: DEBUG/dalvikvm(1336): DestroyJavaVM shutting VM down
01-29 21:53:47.105: DEBUG/dalvikvm(1336): HeapWorker thread shutting down
01-29 21:53:47.115: DEBUG/dalvikvm(1336): HeapWorker thread has shut down
01-29 21:53:47.125: DEBUG/jdwp(1336): JDWP shutting down net...
01-29 21:53:47.125: INFO/dalvikvm(1336): Debugger has detached; object registry had 1 entries
01-29 21:53:47.125: DEBUG/dalvikvm(1336): VM cleaning up
01-29 21:53:47.176: ERROR/AndroidRuntime(1336): ERROR: thread attach failed
01-29 21:53:47.195: DEBUG/ActivityManager(63): Uninstalling process org.pixeroid.gtd
01-29 21:53:47.215: DEBUG/dalvikvm(1336): LinearAlloc 0x0 used 623916 of 5242880 (11%)
01-29 21:53:47.474: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f0700e5
01-29 21:53:47.504: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f020031
01-29 21:53:47.504: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f020030
01-29 21:53:47.504: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f050000
01-29 21:53:47.535: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f060000
01-29 21:53:47.584: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f060001
01-29 21:53:47.785: DEBUG/dalvikvm(141): GC freed 294 objects / 13368 bytes in 327ms
01-29 21:53:48.155: DEBUG/dalvikvm(63): GC freed 4525 objects / 319208 bytes in 399ms
01-29 21:53:48.485: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f0700e5
01-29 21:53:48.514: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f020031
01-29 21:53:48.514: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f020030
01-29 21:53:48.514: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f050000
01-29 21:53:48.565: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f060000
01-29 21:53:48.595: WARN/ResourceType(63): Resources don't contain package for resource number 0x7f060001
01-29 21:53:48.895: DEBUG/AndroidRuntime(1347): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-29 21:53:48.977: DEBUG/AndroidRuntime(1347): CheckJNI is ON
01-29 21:53:49.504: DEBUG/AndroidRuntime(1347): --- registering native functions ---
01-29 21:53:49.905: DEBUG/ddm-heap(1347): Got feature list request
01-29 21:53:50.425: INFO/ActivityManager(63): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.pixeroid.gtd/.MainAct }
01-29 21:53:50.505: INFO/ActivityManager(63): Start proc org.pixeroid.gtd for activity org.pixeroid.gtd/.MainAct: pid=1353 uid=10029 gids={1015}
01-29 21:53:50.514: DEBUG/AndroidRuntime(1347): Shutting down VM
01-29 21:53:50.524: DEBUG/dalvikvm(1347): DestroyJavaVM waiting for non-daemon threads to exit
01-29 21:53:50.534: DEBUG/dalvikvm(1347): DestroyJavaVM shutting VM down
01-29 21:53:50.534: DEBUG/dalvikvm(1347): HeapWorker thread shutting down
01-29 21:53:50.544: DEBUG/dalvikvm(1347): HeapWorker thread has shut down
01-29 21:53:50.544: DEBUG/jdwp(1347): JDWP shutting down net...
01-29 21:53:50.544: INFO/dalvikvm(1347): Debugger has detached; object registry had 1 entries
01-29 21:53:50.555: DEBUG/dalvikvm(1347): VM cleaning up
01-29 21:53:50.585: ERROR/AndroidRuntime(1347): ERROR: thread attach failed
01-29 21:53:50.645: DEBUG/dalvikvm(1347): LinearAlloc 0x0 used 639500 of 5242880 (12%)
01-29 21:53:50.864: DEBUG/ddm-heap(1353): Got feature list request
01-29 21:53:51.494: DEBUG/AndroidRuntime(1353): Shutting down VM
01-29 21:53:51.494: WARN/dalvikvm(1353): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-29 21:53:51.504: ERROR/AndroidRuntime(1353): Uncaught handler: thread main exiting due to uncaught exception
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pixeroid.gtd/org.pixeroid.gtd.MainAct}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099648
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.os.Looper.loop(Looper.java:123)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at java.lang.reflect.Method.invoke(Method.java:521)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at dalvik.system.NativeStart.main(Native Method)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099648
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:587)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at org.pixeroid.gtd.MainAct.onCreate(MainAct.java:45)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-29 21:53:51.535: ERROR/AndroidRuntime(1353): ... 11 more
01-29 21:53:51.554: INFO/Process(63): Sending signal. PID: 1353 SIG: 3
01-29 21:53:51.554: INFO/dalvikvm(1353): threadid=7: reacting to signal 3
01-29 21:53:51.554: ERROR/dalvikvm(1353): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
01-29 21:53:55.305: INFO/Process(1353): Sending signal. PID: 1353 SIG: 9
01-29 21:53:55.334: INFO/ActivityManager(63): Process org.pixeroid.gtd (pid 1353) has died.
01-29 21:53:55.365: INFO/UsageStats(63): Unexpected resume of com.android.launcher while already resumed in org.pixeroid.gtd
01-29 21:53:55.494: WARN/InputManagerService(63): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43c98060
答案 0 :(得分:5)
我想要做的就是在布局文件中定义一个视图,并使用ID将其添加到我的选项卡中。
如果你在问题中查看XML文件,你会注意到你没有TabHost
,没有TabWidget,没有FrameLayout
。我将假设这些是您拒绝在您的问题中包含的第三个XML文件。
按ID添加仅适用于FrameLayout
的现有子项,例如this sample project。
您可能想要做的是使用<include>
中的FrameLayout
元素加载其他文件,或者只是将其内容复制到FrameLayout
中并删除单独的文件。
答案 1 :(得分:2)
它不会从官方文档中跳出来,但是如果您将视图ID传递给setContent()
,则应在setup()
之前致电TabHost
,然后再致电addTab()
。