2012-09-10 78 views
1

我已經成功地設置了一個使用ActionBarSherlock的ActionBar,它設置了當我選擇它時啓動的意圖。啓動意圖導致ClassCastException

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_collection: 
      startActivity(new Intent(this, Collection.class)); 
      return true; 
     case R.id.menu_wishlist: 
      startActivity(new Intent(this, Wishlist.class)); 
      return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

的問題是,當這個意圖應該開始我得到這個錯誤:

09-10 20:30:15.496: E/AndroidRuntime(334): FATAL EXCEPTION: main 
09-10 20:30:15.496: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mypackage.thing/com.mypackage.thing.Wishlist}: java.lang.ClassCastException: com.mypackage.thing.Wishlist 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-10 20:30:15.496: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method) 
09-10 20:30:15.496: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507) 
09-10 20:30:15.496: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-10 20:30:15.496: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
09-10 20:30:15.496: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method) 
09-10 20:30:15.496: E/AndroidRuntime(334): Caused by: java.lang.ClassCastException: com.mypackage.thing.Wishlist 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
09-10 20:30:15.496: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 
09-10 20:30:15.496: E/AndroidRuntime(334): ... 11 more 

是有點不妥的應該啓動或在XML文件中的類?我正在使用支持包以及ActionBarSherlock。我試着檢查清單和佈局,但沒有什麼似乎是錯的。此外,我很新的碎片,所以不知道如果設置正確。這是應該啓動類有:然後

... 
    import com.actionbarsherlock.app.SherlockListFragment; 
    import com.mypackage.thing.contentprovider.DatabaseProvider; 
    import com.mypackage.thing.database.WishlistTable; 

    public class Wishlist extends SherlockListFragment 
       implements LoaderManager.LoaderCallbacks<Cursor> { 

    ... 

    //Inflate ListFragment view 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.wishlist, container, false); 
    } 

    ... 

類被設置爲使用cursorLoader和供應商/ CursorAdapter的填充從SQLite的列表。

+1

'WishList'是一個片段不是一個活動! – Luksprog

回答

2

您的WishList類延伸SherlockListFragment。這不是一個Activity類(見ListFragment

爲了把這個在Activity,你必須創建一個新的FragmentActivity(或者更確切地說,SherlockFragmentActivity)承載此片段。This documentation可以幫助你。

+0

謝謝!片段看起來很有用,但之前沒有用過。 – neutron

+0

自從Android 3.x以來,它們非常有用,它們已經成爲佈局的麪包和黃油。 ,好好學習!:) – Eric