繁体   English   中英

Android示例中的tabhost强制在模拟器中关闭

[英]tabhost in Android example force closing in emulator

我写了一个简单的tabhost示例,如下所示

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <TextView
                    android:id="@+id/tv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

这是在main.xml中,所以我将setcontentview设置为main.xml

public class AndroidtabhostActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

它在android 4.0模拟器中工作正常,但是当我在android 2.1模拟器上运行它时,它会强制关闭ddms中的这些日志

03-16 17:19:08.295: E/AndroidRuntime(1579):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
03-16 17:39:27.016: E/AndroidRuntime(2092): Uncaught handler: thread main exiting due to uncaught exception
03-16 17:39:27.036: E/AndroidRuntime(2092): java.lang.RuntimeException: Unable to start activity ComponentInfo{banana.com/banana.com.AndroidtabhostActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.os.Looper.loop(Looper.java:123)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at java.lang.reflect.Method.invoke(Method.java:521)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at dalvik.system.NativeStart.main(Native Method)
03-16 17:39:27.036: E/AndroidRuntime(2092): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.TabActivity.onContentChanged(TabActivity.java:105)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.Activity.setContentView(Activity.java:1622)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at banana.com.AndroidtabhostActivity.onCreate(AndroidtabhostActivity.java:11)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 17:39:27.036: E/AndroidRuntime(2092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-16 17:39:27.036: E/AndroidRuntime(2092):     ... 11 more

为什么会这样?有人可以解释吗

像萨米尔所说的那样改变之后,我仍然有一个例外

03-16 17:50:03.925: E/AndroidRuntime(2372): Uncaught handler: thread main exiting due to uncaught exception
03-16 17:50:03.936: E/AndroidRuntime(2372): java.lang.NullPointerException
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.os.Looper.loop(Looper.java:123)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at java.lang.reflect.Method.invoke(Method.java:521)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-16 17:50:03.936: E/AndroidRuntime(2372):     at dalvik.system.NativeStart.main(Native Method)

Android的Tabhost有问题。 请检查我的博客以获取更多信息以创建标签

搜索tabhost以立即获取相应的信息。

我必须设置至少一个选项卡,否则它会在2.1中给出错误,而4.0会在没有该选项的情况下进行处理,因此我在setcontentview之后添加了此代码

TabHost th = (TabHost) findViewById(R.id.mytabhost);
    th.setup();
    TabSpec ts = th.newTabSpec("whatevver");
    ts.setContent(R.id.tab1);
    ts.setIndicator("TAB1");
    th.addTab(ts); 

并且在两个仿真器中都运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM