【问题标题】:tabHost in android gives Unfortunately,app has stopped errorandroid中的tabHost给出了不幸的是,应用程序已停止错误
【发布时间】:2013-07-02 12:47:23
【问题描述】:

我想要我的应用程序中有两个选项卡,它们位于其他活动中,我通过一个按钮从我的主活动访问。

将控制选项卡的我的活动具有如下代码

    public class Myclass extends TabActivity


{


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myclass);


     TabHost   tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec firstSpec = tabHost.newTabSpec("First");
        firstSpec.setIndicator("first Tab",null);
        Intent firstintent = new Intent(this,Tab1.class);
        firstSpec.setContent(firstintent);


        TabSpec secondSpec = tabHost.newTabSpec("Second");
        secondSpec.setIndicator("first Tab",null);
        Intent secondintent = new Intent(this,Tab2.class);
        secondSpec.setContent(secondintent);


        tabHost.addTab(firstSpec);
        tabHost.addTab(secondSpec);
    }
}





And the XML which has the tabHost content has the code

    <?xml version="1.0" encoding="utf-8"?>


    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabHost"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true">

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


            </FrameLayout>
        </LinearLayout>
    </TabHost>


So the app is compiling and loading up fine in my device and able to access other activites. But when I try to access the button which should display the activity above mentioned "Myclass" activity. The application exits and I get a screen displaying "Unfortunately,app has stopped".


I have entered the activities in the AndroidManifest.xml also.


The TabActivity shows a cancel mark on the word and says 'android.app.TabActivity' is deprecated.

What does this mean exactly?




Can any one suggest me what is going wrong with the program.













I changed the code as given in the  link http://developer.android.com/reference/android/app/TabActivity.html to the code below                                                   Code 

    public class MyClass extends FragmentActivity


    {

        private FragmentTabHost mTabHost;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.myclass);
            mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

            mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                   Tab1.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                    Tab2.class, null);
        }
    }

以及 XML 代码

<android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>

        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>

        <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

但还是没有效果,同样的问题是点击按钮启动Activity Myclass时应用程序退出。

谁能提出解决方案?

【问题讨论】:

    标签: android android-tabhost tabactivity android-framelayout


    【解决方案1】:

    你应该改变

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    

    与

    TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
    

    【讨论】:

      【解决方案2】:

      试试这个:

      在您的 XML 中进行更改

      更改选项卡主机 ID

      android:id="@+id/tabHost" 到 android:id="@android:id/tabhost"

      应该是这样的:

      <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@android:id/tabhost
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-28
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多