简体   繁体   中英

Wrong 2nd Argument, Found: “com.test.me”, required “android.support.v4.app.Fragment” when using replace (with fragment transaction)

MainActivity fragment1 = new MainActivity();
    android.support.v4.app.FragmentTransaction fragmentTransaction =
            getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment1);
    fragmentTransaction.commit();

My MainActivity extends FragmentActivity. But replace wants the 2nd argument type to be android.support.v4.app.Fragment... I did a lot of googling on this and none of the solutions helped me. I don't want to change MainActivity so it extends Fragment instead - that would give me more problems but it would fix this one.

Is there any way I could put a cast on 'fragment1' in some way? I'm not sure how to tackle this problem. Thank you.

Your fragment1 is an Activity not a Fragment , and as the error says, it expects a Fragment . If you really don't want to change for it to extend Fragment , then launch it as an activity

public void launchMainActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

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