简体   繁体   中英

Why can I not cast from Fragment to MyOwnFragment?

I've got a fragment which I obviously extend from the fragment class as follows:

public class MyOwnFragment extends Fragment {

Then, in my LoginActivity (defined as public class LoginActivity extends FragmentActivity { ), I try to call this fragment like so:

final MyOwnFragment thefragment = (MyOwnFragment) getSupportFragmentManager()
    .findFragmentById(R.id.fragment_my_own);

Unfortunately, this results in an error saying:

Cannot cast from Fragment to MyOwnFragment

I would not know why this is. Anybody any idea how I can solve this?

From this link Here Please make sure that you import correct Fragment class. Please try to Import

android.support.v4.app.Fragment

instead of

android.app.Fragment

I hope this will solve your problem.

您正在尝试通过调用getSupportFragmentManager尝试获取Fragment ,或者尝试在getFragmentManager() android.app.Fragment更改为android.support.v4.app.Fragment

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