I’ve successfully setup an ActionBar using ActionBarSherlock and it sets intents to start when I select an option on it.
@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);
}
The problem is that when this intent is supposed to start I get this error:
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
Is something wrong with the class that’s supposed to initiate or in the xml files? i’m using the support package along with ActionBarSherlock. I’ve tried checking the manifest and layouts but nothing seems to be wrong there. Also I’m very new to fragments so not sure if thats set up correctly. The class that’s supposed to start has:
...
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);
}
...
The class is then set up to use a cursorLoader and a provider/cursorAdapter to populate a list from SQLite.
Your
WishListclass extendsSherlockListFragment. This is not anActivityclass (seeListFragment.To put this in an
Activity, you must create a newFragmentActivity(or rather,SherlockFragmentActivity) that hosts this fragment. This documentation may help you.