0

I have problem starting an intent that extends Fragment, from a class that extends Activity.

TabBar2.class --> extends Activity

Favourite.class --> extends Fragment

This is how I write my intent.

Intent intent1 = new Intent(TabBar2.this, Favourite.class);
startActivity(intent1);

But my app crashes when I run the intent when onClick. My logCat says

Unable to instantiate activity
 ComponentInfo{com.honey.test/com.honey.test.Favourite}:
 java.lang.ClassCastException: com.honey.test.Favourite

What did I do wrong? Can someone guide me on how to solve this?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Honey H
  • 299
  • 1
  • 6
  • 23
  • Check thi one `http://stackoverflow.com/questions/9831728/start-a-fragment-via-intent-within-a-fragment` – Stephen Jan 08 '15 at 07:58

1 Answers1

7

It is because you can't call Fragments via Intent, Fragment is a part of an FragmentActivity

All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Favourite) in that, and then call

Intent intent1 = new Intent(TabBar2.this, SomeFragmentActivity.class);
startActivity(intent1);

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity more information

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115