Error: The method replace (int, Fragment) in the type fragmenttransaction are not applicable for the arguments (int, myfragment)

Source: Internet
Author: User

Fragment newfragment =new myfragment ();
Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit ();

Hint Error: The method replace (int, Fragment) in the type fragmenttransaction are not applicable for the arguments (int, myfragmen T

Mom eggs, looking for a long time! Always assume that a subclass object cannot be assigned to a parent class reference. This science!

Error code:

 Packagecom.example.testforfragment;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.fragment.*;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Fragmentmanager Fragmentmanager=Getfragmentmanager (); Fragmenttransaction fragmenttransaction=fragmentmanager.begintransaction (); Fragment newfragment=Newmyfragment ();    Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

Modify:

 Packagecom.example.testforfragment;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.Menu; Public classMainactivityextendsfragmentactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Fragmentmanager Fragmentmanager=Getsupportfragmentmanager (); Fragmenttransaction fragmenttransaction=fragmentmanager.begintransaction (); Fragment newfragment=Newmyfragment ();    Fragmenttransaction.replace (r.layout.activity_main,newfragment). commit (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

1. Imported Packages

2.extends

3.getSurpport ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.