努力应对上下文膨胀回收器视图列表项



在遵循了许多关于RecyclerViews的教程之后,我一直让我的应用程序在这条线上崩溃:

View view = inflater.inflate(R.layout.layout_listitem, parent, false);

这可能是由于上下文为空而导致膨胀对象为空:

LayoutInflater inflater = LayoutInflater.from(context);

但是,我不明白如果我在回收器视图适配器的构造函数中传递上下文,为什么它是空的:

public RecycleViewAdapter(Context context) {
this.context = context;    
}

从我的片段活动中:

View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.matches);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new RecycleViewAdapter(getActivity()));
return view;

我已经尝试了getContext((,getActivity((,我什至覆盖onAttach 方法从那里获取上下文并且仍然崩溃。我是使用片段的新手,所以我真的不知道我在做什么,只是使用人们在论坛上尝试过的解决方案。

日志猫:

09-07 09:48:14.433 7594-7594/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: maginate.net.thebestrapper, PID: 7594
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:722)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at maginate.net.thebestrapper.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:40)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:16916)
at android.view.ViewGroup.layout(ViewGroup.java:5405)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2413)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2122)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6942)
at 

方法 1:- 使用适配器构造函数传递父上下文,然后使用构造函数上下文。

方法2:- 像这样使用父上下文:-

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(context).inflate(R.layout.anylayout, parent, false);
vh = new RowVH(v);
return vh;
}

您无需将Context实例传递给Adapter

确保只尝试在适配器的onCreateViewHolder()方法中绑定布局,您可以从parent参数中获取Context

new YourViewHolderClass(LayoutInflater.from(parent.getContext()).inflate(R.layout.your_layout_xml, parent, false));

相关内容

  • 没有找到相关文章

最新更新