SwipeTab返回Action Bar必须有一个回调异常

时间:2014-02-28 14:28:41

标签: java android eclipse android-fragments tabs

您好我的滑动标签活动

出错了
  

操作栏必须有回调

我相信我遵循了正确的实施方法,但仍然会收到此错误。

  

02-28 22:21:27.709:E / AndroidRuntime(5812):致命异常:主要   02-28 22:21:27.709:E / AndroidRuntime(5812):java.lang.RuntimeException:无法启动活动ComponentInfo {com.thesis.docdroid / com.thesis.docdroid.MainActivity}:java.lang.IllegalStateException:Action栏标签必须有回调   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread.access $ 600(ActivityThread.java:138)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.os.Handler.dispatchMessage(Handler.java:99)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.os.Looper.loop(Looper.java:137)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread.main(ActivityThread.java:4929)   02-28 22:21:27.709:E / AndroidRuntime(5812):at java.lang.reflect.Method.invokeNative(Native Method)   02-28 22:21:27.709:E / AndroidRuntime(5812):at java.lang.reflect.Method.invoke(Method.java:511)   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:798)   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)   02-28 22:21:27.709:E / AndroidRuntime(5812):at dalvik.system.NativeStart.main(Native Method)   02-28 22:21:27.709:E / AndroidRuntime(5812):引起:java.lang.IllegalStateException:Action Bar Tab必须有一个回调   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.android.internal.app.ActionBarImpl.configureTab(ActionBarImpl.java:549)   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:575)   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:563)   02-28 22:21:27.709:E / AndroidRuntime(5812):at com.thesis.docdroid.MainActivity.onCreate(MainActivity.java:41)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.Activity.performCreate(Activity.java:5254)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)   02-28 22:21:27.709:E / AndroidRuntime(5812):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)   02-28 22:21:27.709:E / AndroidRuntime(5812):... 11更多

这是我的代码:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity implements TabListener {
    private String selectedProfileName;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
        setContentView(R.layout.main_activity);

        Bundle args = getIntent().getExtras();
        selectedProfileName = args.getString("key_name");

        InitializeActionBar();

    }

    public void InitializeActionBar() {
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tabBodyPart = actionBar.newTab();
        tabBodyPart.setText("Body Parts");
        tabBodyPart.setTabListener(MainActivity.this);

        ActionBar.Tab tabGeneralSymptoms = actionBar.newTab();
        tabBodyPart.setText("General Symptoms");
        tabBodyPart.setTabListener(MainActivity.this);

        ActionBar.Tab tabSelectedSymptoms = actionBar.newTab();
        tabBodyPart.setText("Selected Symptoms");
        tabBodyPart.setTabListener(MainActivity.this);

        actionBar.addTab(tabBodyPart);
        actionBar.addTab(tabGeneralSymptoms);
        actionBar.addTab(tabSelectedSymptoms);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

2 个答案:

答案 0 :(得分:1)

复制面食错误。

你有:

tabBodyPart.setText("General Symptoms");
tabBodyPart.setTabListener(MainActivity.this);

你想:

tabGeneralSymptoms.setText("General Symptoms");
tabGeneralSymptoms.setTabListener(MainActivity.this);

答案 1 :(得分:0)

您正在使用v4库不支持的操作栏,而是使用v7。使用ActionBarActivty扩展FragmentActivity