Android:在适配器上显示dialogFrag会产生NullPointerException(Android: showing dialogFrag on adapter gives NullPo

编程入门 行业动态 更新时间:2024-10-22 09:29:58
Android:在适配器上显示dialogFrag会产生NullPointerException(Android: showing dialogFrag on adapter gives NullPointerException)

我正在尝试通过适配器打开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说的那样,除非你确实需要它,否则只需将上下文设置为直接而不是其父项,这就是Nul​​l的潜在原因。

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.

更多推荐

本文发布于:2023-08-04 22:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适配器   dialogFrag   Android   adapter   showing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!