为什么我的tabhost意图不起作用?

时间:2014-09-04 13:57:32

标签: android android-intent android-tabhost

这是我的开始活动,我想在点击标签时打开其他活动,其他活动也在android清单中说明,请告诉我我做错了什么,提前谢谢:

package at.co.ccc.mondel;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends ActionBarActivity {

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

        TabHost tabs = (TabHost)findViewById(R.id.tabhost);
        tabs.setup();

        TabHost.TabSpec spec = tabs.newTabSpec("Simmering");
        Intent intent = new Intent(this, Simmering.class);
        spec.setContent(intent);
        spec.setIndicator("Simmering");
        tabs.addTab(spec);

        spec = tabs.newTabSpec("Lugner City");
        Intent intent2 = new Intent(this, LugnerCity.class);
        spec.setContent(intent2);
        spec.setIndicator("Lugner City");
        tabs.addTab(spec);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这里是xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

2 个答案:

答案 0 :(得分:0)

我认为这会对你有帮助

public class MainActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TabHost tabHost = getTabHost();
    TabSpec LugnerCity = tabHost.newTabSpec("Lugner City");
    LugnerCity.setIndicator("Simmering");
    Intent LugnerCityIntent = new Intent(MainActivity.this,
            LugnerCity.class);
    LugnerCity.setContent(LugnerCityIntent);

    // Tab for Songs
    TabSpec Simmering = tabHost.newTabSpec("Simmering");
    // setting Title and Icon for the Tab
    Simmering.setIndicator("Simmering");
    Intent SimmeringIntent = new Intent(this, LugnerCity.class);
    Simmering.setContent(SimmeringIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(Simmering); // Adding Simmering tab

    tabHost.addTab(LugnerCity); // Adding Lugner City

   }

}

和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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".960" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".040" >
    </TabWidget>
</LinearLayout>

</TabHost>

并在清单文件中。

<activity android:name="com.example.asdf.LugnerCity"> </activity>
<activity android:name="com.example.asdf.Simmering"> </activity>

答案 1 :(得分:0)

高于一个xml会给你默认的一个android tabHost看看下面的代码,它会根据你的需要提供一个TabHost -

<?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: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" />

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