-1

I try to build a method that change fragments in frame of activity. in one of the activities its going well, but in the second activity I've got Error. I've copied the code from the first activity and change the relevant things but in the second part I get error that said

Error:(65, 25) error: no suitable method found for add(int,MyCarView)
method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; MyCarView cannot be converted to Fragment)

the code that working is:

fragment2  = new MyCarEdit();

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.myCarFrame,fragment2);
        transaction.commit();

and the second part of the code that NOT working is-

fragment3 = new MyCarView();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction2 = fragmentManager.beginTransaction();
transaction2.add**(R.id.myCarFrame2,fragment3)**;
transaction2.commit();

** = is the part that marked in error

like I said before, I already put this codes in other activity and they worked fine.

What's the problem here?

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Ortal D
  • 63
  • 1
  • 5
  • 1
    The error says: "MyCarView cannot be converted to Fragment" - Is your car view a `Fragment`? – JimmyB Jul 04 '16 at 15:58
  • Yes. "MyCarView" is a fragment, exactly like "MyCarEdit", and I don't understand what the different between them... – Ortal D Jul 04 '16 at 16:13
  • 1
    Are you using `android.support.v4.app.Fragment` or `android.app.Fragment`? Make sure you use the same in both. Take a look here: http://stackoverflow.com/questions/25744681/android-adding-a-simple-fragment – Daniel Nugent Jul 04 '16 at 16:21
  • Double check @JimmyB 's comment. It really looks like MyCarView is not really a fragment. Could you update the question with your MyCarView class code? – Chisko Jul 04 '16 at 16:29
  • You all alright, its not was the same Fragment type. now I change it and its worked. Thank you!!! – Ortal D Jul 04 '16 at 16:56

1 Answers1

0

Besides using the right classes, you are not supposed to create Fragments in the way you are doing it. Google's guidelines for creating fragments is you need to have a public static Fragment newInstance() method which creates instances along with arguments, and your constructor should be empty. Something 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() {}

    ...
}
Chisko
  • 3,092
  • 6
  • 27
  • 45
  • No prob! Using this pattern will allow you to pass arguments into your fragments. Regarding your issue, I'm 99.9% sure @Daniel Nugent's answer will solve it. Please consider upvoting if you find answers useful. – Chisko Jul 04 '16 at 17:03
  • yes, your answer and @Daniel Nugent's answer helps me to find my error. I vote for you but I don't have enough reputation so you don't see that. – Ortal D Jul 04 '16 at 17:15