Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I instantiate this Fragment on Android?

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.

like image 675
Ethan Allen Avatar asked Nov 27 '22 11:11

Ethan Allen


1 Answers

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).

like image 111
CommonsWare Avatar answered Dec 06 '22 11:12

CommonsWare