1

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

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

    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>

但是仍然没有效果,单击按钮启动活动 Myclass 时应用程序退出的相同问题继续存在。

任何人都可以提出解决方案吗?

4

2 回答 2

2

你应该改变

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

TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
于 2013-07-02T12:51:44.463 回答
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">
于 2013-07-02T12:57:55.280 回答