Search code examples
androidandroid-intentandroid-tabhost

why does my tabhost with intent not work?


here is my start activity, i want other activities to open when tab is clicked, the other activities are also stated in the android manifest, please tell me what i am doing wrong, thank you in advance:

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);
    }
}

and here the 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>

Solution

  • I think this would help you

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

    and the xml file is.

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

    and in the manifest file.

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