简体   繁体   English

片段内DialogFragment内的RecyclerView

[英]RecyclerView inside DialogFragment inside Fragment

I am trying to inflate RecyclerView inside the DialogFragment which is Inside a Fragment. 我正在尝试在Fragment里面的DialogFragment里面充气RecyclerView。 I made RecyclerView Adapter normally as per guidelines. 我通常按​​照指南制作了RecyclerView适配器。

But I am getting this error java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroid.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference 但是我收到此错误java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroid.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

On the line 在线上

recyclerView.setLayoutManager(manager); recyclerView.setLayoutManager(经理);

Here's the complete code 这是完整的代码

public class AlertFragment extends DialogFragment {
RecyclerView recyclerView;
RecyclerView.Adapter adapter2;
RecyclerView.LayoutManager manager;

ArrayList<String> goalname = new ArrayList<String>();
 ArrayList<String> goalcategory = new ArrayList<String>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_alert, container, false);
    goalname.add("Running");
    goalcategory.add("Physical");
    goalname.add("Yoga");
    goalcategory.add("Physical");
    goalname.add("Gym");
    goalcategory.add("Physical");
    goalname.add("Meditation");
    goalcategory.add("Mental");
    goalname.add("Puzzle Solving");
    goalcategory.add("Habits");
    manager = new LinearLayoutManager(getActivity());
    recyclerView = (RecyclerView) getActivity().findViewById(R.id.bookhistory);
    recyclerView.setLayoutManager(manager);


    adapter2 = new GoalListAdapter(goalname, goalcategory,getActivity());
    recyclerView.setAdapter(adapter2);
    adapter2.notifyDataSetChanged();
    return v;
}}

Obviously it is going to crash, because it's not finding recycler view instance. 显然它将崩溃,因为它没有找到回收站视图实例。

Just replace this line: 只需替换此行:

  recyclerView = (RecyclerView) getActivity().findViewById(R.id.bookhistory); 

With: 附:

 recyclerView = (RecyclerView) v.findViewById(R.id.bookhistory); 

Now it will work for you. 现在它将为您工作。

Your issue is with this line: 您的问题是与此行:

    recyclerView = (RecyclerView) getActivity().findViewById(R.id.bookhistory);

You really want to get the RecyclerView from the view that you inflated in your fragment on this line 您确实想从您在此行的片段中放大的视图中获取RecyclerView

    View v = inflater.inflate(R.layout.fragment_alert, container, false);

So to fix this, change the first line of code I mentioned to 因此,要解决此问题,请更改我提到的第一行代码

    recyclerView = (RecyclerView) v.findViewById(R.id.bookhistory);

This is assuming that the DialogFragment contains bookhistory 这是假定DialogFragment包含书籍历史记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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