我的Android应用程序由于某种原因没有启动。这是一个非常基本的应用程序。当我运行它时,它只是说它必须关闭并且我强行关闭。
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.*;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class MainActivity extends SherlockFragmentActivity implements TabListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabRed = actionBar.newTab();
tabRed.setText("Red Side");
// tabRed.setTabListener(new TabListener<TabFragment>(this, "tab1", TabFragment.class));
Tab tabBlue = actionBar.newTab();
tabBlue.setText("Blue Side");
// tabBlue.setTabListener(new TabListener<TabFragment>(this, tabBlue, TabFragment.class));
actionBar.addTab(tabRed);
actionBar.addTab(tabBlue);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Log.d("Main Activity", "tab " + String.valueOf(tab.getPosition()) + " clicked");
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Log.d("Main Activity", "tab " + String.valueOf(tab.getPosition())+ " un-clicked");
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Log.d("Main Activity", "tab " + String.valueOf(tab.getPosition()) + " re-clicked");
}
}
这是我的activity_main.xml
,其中只有文字。我是否还必须在xml中设置操作栏?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
logcat
12-22 00:19:37.846: E/AndroidRuntime(1833): FATAL EXCEPTION: main
12-22 00:19:37.846: E/AndroidRuntime(1833): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.say.upscorer/org.say.upscorer.MainActivity}: java.lang.IllegalStateException: Action Bar Tab must have a Callback
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.os.Looper.loop(Looper.java:123)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 00:19:37.846: E/AndroidRuntime(1833): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 00:19:37.846: E/AndroidRuntime(1833): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 00:19:37.846: E/AndroidRuntime(1833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-22 00:19:37.846: E/AndroidRuntime(1833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-22 00:19:37.846: E/AndroidRuntime(1833): at dalvik.system.NativeStart.main(Native Method)
12-22 00:19:37.846: E/AndroidRuntime(1833): Caused by: java.lang.IllegalStateException: Action Bar Tab must have a Callback
12-22 00:19:37.846: E/AndroidRuntime(1833): at com.actionbarsherlock.internal.app.ActionBarImpl.configureTab(ActionBarImpl.java:424)
12-22 00:19:37.846: E/AndroidRuntime(1833): at com.actionbarsherlock.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:450)
12-22 00:19:37.846: E/AndroidRuntime(1833): at com.actionbarsherlock.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:438)
12-22 00:19:37.846: E/AndroidRuntime(1833): at org.say.upscorer.MainActivity.onCreate(MainActivity.java:33)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-22 00:19:37.846: E/AndroidRuntime(1833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-22 00:19:37.846: E/AndroidRuntime(1833): ... 11 more
答案 0 :(得分:0)
每个标签必须有一个TabListener,例如:
Tab tabRed = actionBar.newTab();
tabRed.setText("Red Side");
tabRed.setTabListener(this);
actionBar.addTab(tabRed);
// The same is true for tabBlue