android - android 中的 tabHost 给出 Unfortunately,app has stopped 错误

标签 android android-tabhost tabactivity android-framelayout

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

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

    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>

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

谁能提出解决方案?

最佳答案

你应该改变

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

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

关于android - android 中的 tabHost 给出 Unfortunately,app has stopped 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426049/

相关文章:

Android UI TabActivity 问题

android - 具有DiffUtil和LiveData的RecyclerView

java - Android 中的 Button 关键字错误

android - 在 android 中禁用 DPAD 键

android - 为什么 Eclipse 突出显示 R.id.tabhost?

Android SearchView 不允许输入(与 TabActivity 冲突?)

android - 如何在 android 应用程序中使用外部 sqlite 数据库

Android SDK - 使用手势API,希望手势在被识别后留在屏幕上

android - 无法解析 R.id。安卓。

android - 父 Activity 中的 onKeyDown 或 onBackPressed