2012-12-22 70 views
1

我的Android應用程序由於某種原因未啓動。這是一個非常基本的應用程序。當我運行它時,它只是說它必須關閉,並且我逼着關閉。Action Bar Sherlock未啓動

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

按Ctrl + Shift + O來解決所有的進口。確保您使用的是ActionBarSherlock附帶的「android支持jar」,並確保將ActionBarSherlock添加到您的項目的項目庫中。 –

回答

0

每個標籤頁必須有一個TabListener,例如:

Tab tabRed = actionBar.newTab(); 
tabRed.setText("Red Side"); 
tabRed.setTabListener(this); 
actionBar.addTab(tabRed); 

// The same is true for tabBlue 
+0

感謝它現在的作品 – rasen58

+0

沒有touble。它花了我一會兒,但這是[源代碼](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/com/android/內部/應用程序/ ActionBarImpl.java#ActionBarImpl.configureTab%28android.app.ActionBar.Tab%2Cint%29)引發這個特定的錯誤。 – Sam