我正在尝试通过适配器打开dialogFragment,我已经遍布文档和其他问题,我找不到问题。 这是来自日志java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentManager android.app.Activity.getFragmentManager()' on a null object reference的完整错误行java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentManager android.app.Activity.getFragmentManager()' on a null object reference
我的适配器:
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(); }好吧,当我点击GridButton时,我在这里缺少什么来显示我的DialogFragment?
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?
最满意答案
在这一行:
newFragment.show(mcontext.getFragmentManager(), "Title");你要求mcontext为fragmentManager,你的问题是那时mcontext为null。 就像George说的那样,除非你确实需要它,否则只需将上下文设置为直接而不是其父项,这就是Null的潜在原因。
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.
更多推荐
发布评论