带有ActionBar的Android TabHost。不同选项卡的差异操作

时间:2015-05-17 11:22:59

标签: android android-tabhost

在TabSpec上获取错误。我在MainActivity中实现了ActionBar.TabListener。首先mycode在没有ActionBar的情况下工作,但我需要ActionBar来执行不同的活动。例如。如果是主页选项卡,它将在ActionBar上有搜索选项。在下一个标签中,ActionBar上会有另一个活动。

我该怎么做?

这是我的代码:

MainActivity.java

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTabs();
    }

    private void setTabs() {
        addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
        addTab("Search", R.drawable.tab_search, OptionsActivity.class);

        addTab("Home", R.drawable.tab_home, ArrowsActivity.class);
        addTab("Search", R.drawable.tab_search, OptionsActivity.class);
    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        TabHost tabHost = (TabHost) findViewById(R.id.tab_host);
        tabHost.setup();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);



        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, tabHost.getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }
}

logcat的

Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
            at android.widget.TabHost.setCurrentTab(TabHost.java:413)
            at android.widget.TabHost.addTab(TabHost.java:240)
            at dino.myapplication.MainActivity.addTab(MainActivity.java:66)
            at dino.myapplication.MainActivity.setTabs(MainActivity.java:33)
            at dino.myapplication.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:5242)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您正在扩展ArctionBarActivity尝试扩展TabActivity。 或者如果你想使用Activity 然后使用此代码..希望它有效

host = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
    LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
    mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
    tabHost.setup(mLocalActivityManager);

你可以像这样使用OnTabChangedListener来改变ActionBar

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
                @Override
                public void onTabChanged(String tabId) {
                   // Toast.makeText(getApplicationContext(),"tab id"+ tabId, 1000).show();

                    if(tabId.equalsIgnoreCase("Voucher")){
                        // add your code for change ActionBar
                        Toast.makeText(getApplicationContext(),tabId, 1000).show();

                    }else{
                        // add your code for change ActionBar
                    Toast.makeText(getApplicationContext(),tabId, 1000).show();
                }
            }});