简体   繁体   中英

Android: showing dialogFrag on adapter gives NullPointerException

I'm trying to open a dialogFragment through an adapter, I've been all over the documentation and other questions, I can't find the problem. This is the full error line from the log java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentManager android.app.Activity.getFragmentManager()' on a null object reference

My adapter:

public class SubjectsAdapter extends RecyclerView.Adapter<SubjectsAdapter.ViewHolder> {

    public List<String> items = new ArrayList<>();
    public Activity mcontext;

    public SubjectsAdapter(Activity context) {
        this.mcontext=context.getParent();
        assert mcontext != null;

    }

//...usual adapter code here...

    static int i;

    class ViewHolder extends RecyclerView.ViewHolder{

        public Button GridButton;

        public ViewHolder(View itemView) {
            super(itemView);

            GridButton = (Button) itemView.findViewById(R.id.grid_button);

            GridButton.setId(++i);
            GridButton.setText("Button " + i);

            assert GridButton != null;
            GridButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //removeItem(getAdapterPosition());
                    Fragment_Subject_Edit newFragment = Fragment_Subject_Edit.newInstance();
                    newFragment.show(mcontext.getFragmentManager(), "Title");
                }
            });  
        }
    }       
}

the DialogFragment:

     public static Fragment_Subject_Edit newInstance() {

        return new Fragment_Subject_Edit();
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.fragment_subject_edit, null));

        return builder.create();
    }

Ok so what am I missing here to show my DialogFragment when I click in the GridButton?

At this line:

newFragment.show(mcontext.getFragmentManager(), "Title");

You are asking to mcontext for the fragmentManager and your problem is that at that point mcontext is null. Like George said, unless you really need it, just set the context straight ahead instead of its parent, thats a potential cause of the Null.

try using following code after including the Pop library in your gradle dependencies. you wont even need to implement method onCreateDialog

add this in your app module gradle

dependencies {
    compile 'com.vistrav:pop:2.0'
}

and your class with be simple as like this

public class SubjectsAdapter extends RecyclerView.Adapter<SubjectsAdapter.ViewHolder> {

    public List<String> items = new ArrayList<>();
    public Activity mcontext;

    public SubjectsAdapter(Activity context) {
        this.mcontext=context.getParent();
        assert mcontext != null;

    }

//...usual adapter code here...

    static int i;

    class ViewHolder extends RecyclerView.ViewHolder{

        public Button GridButton;

        public ViewHolder(View itemView) {
            super(itemView);

            GridButton = (Button) itemView.findViewById(R.id.grid_button);

            GridButton.setId(++i);
            GridButton.setText("Button " + i);

            assert GridButton != null;
            GridButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Pop.on(mcontext).with().title("Title").body(R.layout.fragment_subject_edit).show();
                }
            });  
        }
    }       
}   

you won't need any more code to build dialog after this. you can check more detail of this library here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM