4

Here is the code I have. I'm getting a red line under new MainFragment(); with the error Type mismatch: cannot convert from WishlistFragment to Fragment:

Fragment newfragment = new MainFragment();   

Here is what my MainFragment.java file looks like:

public class MainFragment extends Fragment {
    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
}

Any ides on why this is happening? I have a feeling it's something stupid I am looking over and I am just code-blind right now from a long day.

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • First, what does Eclipse tell you specifically is the problem? Just "a red line" is not very information. Second, either delete your constructor (since it is not doing anything) or at least chain to the superclass (so `Fragment`'s constructor can do its work). – CommonsWare Jul 11 '12 at 00:01
  • Edited the main questions.... error is: "Type mismatch: cannot convert from WishlistFragment to Fragment" – Ethan Allen Jul 11 '12 at 00:03
  • including the default constructor like that in your code is unnecessary... just remove it :) – Alex Lockwood Jul 11 '12 at 00:13
  • Will do. I had it there because I was running out of ideas as to what could be causing the issue. – Ethan Allen Jul 11 '12 at 00:16

2 Answers2

21

Make sure that both places are importing the same Fragment class. It feels a bit like in one place you are importing android.app.Fragment (the native API Level 11 version of fragments) and in the other places you are importing android.support.v4.app.Fragment (the fragments from the Android Support package).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
8

Figured out the issue.... my MainActivity.java file was using:

import android.support.v4.app.Fragment;

and my MainFragment.java file was using:

import android.app.Fragment;

So there was a mismatch. I am using ICS 4.0 and higher as my lowest API version. Changing everything to import to the v4 API solved my issue.

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194