java - Cannot Resolve Method error Android Studio -


i try build method change fragments in frame of activity. in 1 of activities going well, in second activity i've got error. i've copied code first activity , change relevant things in second part error said

error:(65, 25) error: no suitable method found add(int,mycarview)
method fragmenttransaction.add(fragment,string) not applicable (argument mismatch; int cannot converted fragment)
method fragmenttransaction.add(int,fragment) not applicable (argument mismatch; mycarview cannot converted fragment)

the code working is:

fragment2  = new mycaredit();  fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.add(r.id.mycarframe,fragment2);         transaction.commit(); 

and second part of code not working is-

fragment3 = new mycarview(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction transaction2 = fragmentmanager.begintransaction(); transaction2.add**(r.id.mycarframe2,fragment3)**; transaction2.commit(); 

** = part marked in error

like said before, put codes in other activity , worked fine.

what's problem here?

besides using right classes, not supposed create fragments in way doing it. google's guidelines creating fragments need have public static fragment newinstance() method creates instances along arguments, , constructor should empty. like:

public class yourfragment extends fragment {      public static yourfragment newinstance() {         yourfragment frag = new yourfragment();         bundle args = new bundle();         ...         frag.setarguments(args);         return frag;     }      public yourfragment() {}      ... } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -