简体   繁体   中英

How to start an intent that extends fragment, from a class that extends Activity?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM