アダプタを使用してdialogFragmentを開こうとしていますが、ドキュメントやその他の質問がありましたが、問題は見つかりません。これは、ログからの完全なエラーラインであるjava.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentManager android.app.Activity.getFragmentManager()' on a null object reference
Android:dialogFragをアダプタに表示するとNullPointerExceptionが発生する
マイアダプタ:
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");
}
});
}
}
}
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]をので、どのような私は私DialogFragmentときを示すために、ここで行方不明です私はGridButtonをクリックしますか?このラインで
'mcontext = context;'を指定するとどうなりますか?あなたが現在 'mcontext = context.getParent();'を使っている特別な理由は? –
さて、それは働いて、ありがとう! –