Adrià Illa Adrià Illa - 1 year ago 80
Android Question

Why isn't BlankFragment not working?

I'm trying to create an app that displays a fragment whenever you click an option in a navigation drawer. However, when creating a BlankFragment (by using

BlankFragment fragment = new BlankFragment();
, I get an error:
turns into red and Android Studio doesn't recognize it.


} else if (id == {

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction transaction = fragmentManager.beginTransaction();

BlankFragment fragment = new BlankFragment();
transaction.add(, fragment);


} else if (id == {




import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ShooksFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
public class ShooksFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public ShooksFragment() {
// Required empty public constructor

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shooks, container, false);

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {

public void onAttach(Context context) {
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");

public void onDetach() {
mListener = null;

* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* ""
* >Communicating with Other Fragments</a> for more information.
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);

Here is the error. As you can see, the word BlankFragment is in red

Answer Source

You're missing the newInstance() method in the ShooksFragment,

Add this method and please report back.

// TODO: Rename and change types and number of parameters
public static ShooksFragment newInstance() {

    ShooksFragment fragment = new ShooksFragment();

    return fragment;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download