开发者

unable to add views in TabHost by IDs, crashing

I've created a TabHost example with 1 tab which is running fine. I'm adding views in tabs with TabContentFactory using TabSpec.setContent(TabHost.TabContentFactory contentFactory)

But as soon as I try to add tabs by views in tabs using View/Resource IDs using TabSpec.setContent(int viewId) my app crashes. The code is as follows:

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;
    }
}

My XML files are as follows

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

and

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

Both of them defines IDs properly, yet they crash. I've already gone through following online tutorials:

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/

All of them describes how to add views in tabs using IDs, but none works for me.

All I want to do is to define a view in a layout file and add it inside my tab using IDs. Please help.

EDIT 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:4开发者_StackOverflow中文版7.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


All I want to do is to define a view in a layout file and add it inside my tab using IDs.

If you look at your XML files in your question, you will notice that you have no TabHost, no TabWidget, and no FrameLayout. I will assume that these are in some third XML file that you declined to include in your question.

Adding by ID only works for existing children of the FrameLayout, such as in this sample project.

What you may want to do is use the <include> element inside your FrameLayout to load these other files in, or simply copy their contents into the FrameLayout and eliminate the separate files.


It doesn't jump out from the official documentation but if you're passing view ids to setContent() you should call setup() on the TabHost before calling addTab().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜