在遵循了很多有关RecyclerViews的教程之后,我一直使我的应用程序在此行崩溃:
View view = inflater.inflate(R.layout.layout_listitem, parent, false);
这可能是由于充气机对象为null导致的,因为上下文为null:
LayoutInflater inflater = LayoutInflater.from(context);
但是,如果我在RecyclerViewAdapter的构造函数中传递上下文,我不明白为什么它为null:
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方法来从那里获取上下文,但仍然崩溃。我刚开始使用片段,所以我真的不知道自己在做什么,只是使用人们在论坛上尝试过的解决方案。
Logcat:
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
答案 0 :(得分:1)
方法1:- 使用适配器构造函数传递父上下文,然后使用构造函数上下文。
方法2:- 像这样使用父上下文:-
function debouncer(func, wait, immediate) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
if (!immediate) func.apply(this, args);
}, wait);
if (immediate && !timeout) func.apply(this, args);
};
}
答案 1 :(得分:1)
您无需将Context
实例传递给您的Adapter
。
确保仅尝试在适配器的onCreateViewHolder()
方法中绑定布局,才能从Context
参数中获取parent
:
new YourViewHolderClass(LayoutInflater.from(parent.getContext()).inflate(R.layout.your_layout_xml, parent, false));