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 票数:2

您正在扩展ArctionBarActivity尝试扩展TabActivity 或者,如果您想使用“活动”,请使用此代码。

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

  ask by 89030943809 translate from so

未解决问题?本站智能推荐: