不推荐使用android中的TabActivity没有错误但是单击按钮时应用程序不幸停止了

时间:2015-09-07 19:10:28

标签: android eclipse tabactivity

PS。我是android和java编程的新手。

我在我的应用程序中遇到这个问题当我点击我处理标签活动的意图按钮(不推荐的一个)时它会崩溃,有一些警告没有错误,但我似乎无法弄清楚我做错了什么

这是代码。

InformationActivity.class

style

OfflineTab.class

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class InformationActivity extends TabActivity
{
        /** Called when the activity is first created. */

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_information);

               // create the TabHost that will contain the Tabs
                TabHost tabHost = (TabHost)findViewById(R.id.tabhost);


               TabSpec tab1 = tabHost.newTabSpec("First Tab");
               TabSpec tab2 = tabHost.newTabSpec("Second Tab");


             // Set the Tab name and Activity
             // that will be opened when particular Tab will be selected
                tab1.setIndicator("Tab1");
                tab1.setContent(new Intent(this,OfflineTab.class));

                tab2.setIndicator("Tab2");
               tab2.setContent(new Intent(this,OnlineTab.class));

              /** Add the tabs  to the TabHost to display. */
              tabHost.addTab(tab1);
               tabHost.addTab(tab2);


        }
} 

OnlineTab.class

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class OfflineTab extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv=new TextView(this);
        tv.setTextSize(25);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText("Contains R.E.D. e - Kit Offline module");

        setContentView(tv);
    }
}

activity_information.xml

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class OnlineTab extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv=new TextView(this);
        tv.setTextSize(25);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText("This Is Tab2 Activity");

        setContentView(tv);
    }
 }

1 个答案:

答案 0 :(得分:1)

日志说: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

所以,不要为tabhost定义新的id:

activity_information.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/tabhost"

android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/LinearLayout01"
    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>

InformationActivity的onCraete()

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_information);
           // create the TabHost that will contain the Tabs

            TabHost tabHost = getTabHost(); // <- get tabhost by this


           TabSpec tab1 = tabHost.newTabSpec("First Tab");
           TabSpec tab2 = tabHost.newTabSpec("Second Tab");


         // Set the Tab name and Activity
         // that will be opened when particular Tab will be selected
            tab1.setIndicator("Tab1");
            tab1.setContent(new Intent(this,OfflineTab.class));

            tab2.setIndicator("Tab2");
           tab2.setContent(new Intent(this,OnlineTab.class));

          /** Add the tabs  to the TabHost to display. */
          tabHost.addTab(tab1);
           tabHost.addTab(tab2);

}

它有效! enter image description here