android中的tabHost给了不幸的是,app已经停止了错误

时间:2013-07-02 12:47:24

标签: android android-tabhost tabactivity android-framelayout

我想在我的应用程序中有两个标签,这些标签位于我从主要活动中通过按钮访问的其他活动中。

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

    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 CODE改为

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

但仍然没有效果,点击按钮启动Myclass活动后,应用程序退出的问题仍然存在。

有人可以建议解决方案吗?

2 个答案:

答案 0 :(得分:2)

你应该改变

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

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

答案 1 :(得分:0)

试试这个:

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