TabHost活动无效

时间:2017-10-17 16:06:13

标签: android android-tabhost

我在我的Android应用中实现了TabHost。会有3个标签。点击每个标签我想去不同的活动。但当我点击标签时,它突然停止工作。所以可能会有一些错误。我不明白错误

这里是java文件

public class ToDoListActivity extends AppCompatActivity {


TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_todo);

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

    //Tab 1
    TabHost.TabSpec tab1 = host.newTabSpec("Tab One");
    TabHost.TabSpec tab2 = host.newTabSpec("Tab Two");
    TabHost.TabSpec tab3 = host.newTabSpec("Tab Three");

    tab1.setContent(new Intent(this,Tab1Activity.class));
    //tab1.setContent(R.id.tab1);
    tab1.setIndicator("Tab One");
    host.addTab(tab1);



    //Tab 2
    tab2.setContent(new Intent(this,Tab2Activity.class));
    //tab2.setContent(R.id.tab2);
    tab2.setIndicator("Tab Two");
    host.addTab(tab2);


    //Tab 3
    tab3.setContent(new Intent(this,Tab3Activity.class));
    //tab3.setContent(R.id.tab3);
    tab3.setIndicator("Tab Three");
    host.addTab(tab3);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这里是相应的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.emma.kidbox2.ToDoListActivity">

<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

Error log is here

1 个答案:

答案 0 :(得分:0)

更新您的代码如下:

public class ToDoListActivity extends AppCompatActivity {

TabHost tabHost;
LocalActivityManager mlam;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_todo);

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

    mlam= new LocalActivityManager(this, false);
    mlam.dispatchCreate(savedInstanceState);
    host.setup(mlam);

    //Tab 1
    TabHost.TabSpec tab1 = host.newTabSpec("Tab One");
    TabHost.TabSpec tab2 = host.newTabSpec("Tab Two");
    TabHost.TabSpec tab3 = host.newTabSpec("Tab Three");

    tab1.setContent(new Intent(this,Tab1Activity.class));
    //tab1.setContent(R.id.tab1);
    tab1.setIndicator("Tab One");
    host.addTab(tab1);



    //Tab 2
    tab2.setContent(new Intent(this,Tab1Activity.class));
    //tab2.setContent(R.id.tab2);
    tab2.setIndicator("Tab Two");
    host.addTab(tab2);


    //Tab 3
    tab3.setContent(new Intent(this,Tab1Activity.class));
    //tab3.setContent(R.id.tab3);
    tab3.setIndicator("Tab Three");
    host.addTab(tab3);

}
@Override
protected void onResume(){
    super.onResume();
    mlam.dispatchResume();
}

@Override
protected void onPause(){
    super.onPause();
    mlam.dispatchPause(isFinishing());
}

}