我试图弄乱新的RecyclerView,每当我尝试运行它时,我的应用程序立即崩溃。它试图从android.support.v7.widget.RecyclerView
访问方法给我NullPointerException。我查看了其他帖子,看到大多数人都没有compile 'com.android.support:recyclerview-v7:+'
,但我尝试了这一点,它根本没有帮助。目前还不确定该做什么,任何帮助都将不胜感激。这里的错误日志:(我会张贴一张图片,但我还没有10个代表)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:17430)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
答案 0 :(得分:210)
如果没有为LayoutManager
提供RecyclerView
,则通常会发生此问题。你可以这样做:
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
答案 1 :(得分:26)
在我的情况下,它与'final'无关,而是与@NemanjaKovačević在@aga回答中提到的问题有关。 我在数据加载时设置了一个layoutManager,这也是导致同样崩溃的原因。 将layoutManager设置移动到my片段的onCreateView后,问题就解决了。
这样的事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
...
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler);
mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
答案 2 :(得分:10)
对我来说,我遇到了同样的问题,问题是xml中有一个未使用的RecyclerView,但是我没有将它绑定到Activity中的任何适配器,因此问题就出现了。我在xml
中删除了这些未使用的回收站视图后立即解决了这个问题即 - 我删除了此视图,因为未在代码中调用或已设置任何适配器
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_profileview_allactivities"
android:visibility="gone" />
答案 3 :(得分:8)
即使我已正确设置RecyclerView.LayoutManager
,我也遇到了此次崩溃。我必须将RecyclerView
初始化代码移到onViewCreated(...)
回调中以解决此问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listing, container, false);
rootView.setTag(TAG);
return inflater.inflate(R.layout.fragment_listing, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new ListingAdapter(mListing);
mRecyclerView.setAdapter(mAdapter);
}
答案 4 :(得分:4)
您需要在setLayoutManager
方法中使用RecyclerView#onCreate()
。在将recyclerView
添加到视图之前,必须设置LayoutManager
。
private RecyclerView menuAsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
menuAsList = (RecyclerView) findViewById(R.id.recyclerView_mainMenu);
menuAsList.setLayoutManager(new LinearLayoutManager(Home.this));
}
答案 5 :(得分:2)
由于错误引用了RecyclerView
id。
recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list);
到
recyclerView = (RecyclerView) findViewById(R.id.rv_search_list);
答案 6 :(得分:2)
recyclerView =
(RecyclerView) findViewById(R.id.recycler_view2);
检查回收者视图ID,指向实际回收者视图解决了我的问题
答案 7 :(得分:1)
我的问题出在我的XML lyout中,我将 android:animateLayoutChanges 设置为true,并且我在Java代码中的RecyclerView适配器上调用了notifyDataSetChanged()。
所以,我刚刚从我的布局中删除了 android:animateLayoutChanges ,并重新解决了我的问题。
答案 8 :(得分:1)
使用Butterknife库时遇到此问题。 我有:
View rootView = inflater.inflate
(R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(rootView);
但正确的版本是:
View rootView = inflater.inflate
(R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(this, rootView);
答案 9 :(得分:1)
我使用的是依赖注入-黄油刀,由于未绑定视图,因此出现了此问题。
所以在onCreate()中,我只是插入了这一行:
ButterKnife.bind(this);
我的带有依赖项注入的RecyclerView声明:
@BindView(R.id.recyclerview)
RecyclerView recyclerView;
答案 10 :(得分:0)
在使用Butterknife时遇到了这个问题,在使用碎片
对于Fragment,它应该是ButterKnife.bind(this,view);
对于活动ButterKnife.bind(this);
答案 11 :(得分:0)
我遇到了同样的问题,因为我在另一个布局中而不是在activity_main.xml中使用了RecyclerView。
通过在某些网站上搜索,我得到了一些东西,感觉就像在这里共享
将ID添加到Activity_main.xml中的包含标记
<include id="includedRecyclerLayout"
....
</include>
在MainActivity.java中按ID搜索视图之前调用此布局
`ConstraintLayout includedLayout = findViewById(R.id.inckudedRecyclerLayout);
RecyclerViewrecyclerview = includedLayout.findViewById(R.id.RECYCLER_VIEW_ID)`;
答案 12 :(得分:0)
在GenyMotion上测试应用程序时,我遇到了类似的问题。重启设备后GenyMotion问题得到解决
答案 13 :(得分:0)
对于正在使用的用户:实现'androidx.recyclerview:recyclerview:1.1.0-beta05'
确保您在xml布局中使用它
在API 27以下运行我的应用时,我也遇到了崩溃。Here's a link to the message
最初,我创建了一种样式来更改recyclerview内textview的字体大小,这就是为什么我在下面创建此代码的原因:(当我根据屏幕大小更改字体大小时,我可能只是调用了@dimen)
并将其添加到我的主题中
调查后,我发现这是导致我出错的原因,删除样式后,我不再遇到崩溃。
我尝试了在堆栈溢出中找到的所有可能的解决方案,并几乎放弃了这一点,谢天谢地,出于好奇,我想为什么不删除样式,否则可能导致崩溃,然后崩溃消失了。
答案 14 :(得分:0)
Recyclerview recylerview = findbyid(R.id.recyclerview) 可能是您错过了
答案 15 :(得分:0)
在我的情况下,我仅添加了黄油刀库,却忘记添加注解处理器。通过将以下行添加到build.gradle(App模块)中,解决了我的问题。
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
答案 16 :(得分:0)
在 Gradle 中实施此库:
implementation 'com.android.support:appcompat-v7:SDK_VER'
implementation 'com.android.support:design:SDK_VER'
implementation 'com.android.support:support-v4:SDK_VER'
implementation 'com.android.support:support-v13:SDK_VER'
implementation 'com.android.support:recyclerview-v7:SDK_VER'
在 XML 中提及如下:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"/>
在片段中,我们像这样使用recyclerView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.YOUR_LAYOUT_NAME, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(getActivity(), YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
return rootView;
}
在活动中,我们像这样使用RecyclerView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT_NAME);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
}
如果您需要水平 RecyclerView
,请执行以下操作:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
APICallMethod();
YOURADAPTER = new YOURADAPTER(ACTIVITY_NAME.this, YOUR_LIST); // you can use that adapter after for loop in response(APICALLMETHOD)
recyclerView.setAdapter(YOURADAPTER);
如果您想通过RecyclerView
使用 GridLayoutManager ,请像这样使用:
RecyclerView recyclerView = findViewById(R.id.rvNumbers);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); // 3 is number of columns.
adapter = new MyRecyclerViewAdapter(this, data);
recyclerView.setAdapter(adapter);
答案 17 :(得分:0)
对我来说,问题出在我的activity_main.xml(21)中,其中recycleView没有ID。
activity_main.xml(21)
<android.support.v7.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3"
tools:listitem="@layout/transaction_list_row" />
activity_main.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/transac_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3"
/>
当我在activity_main.xml(21)recycleView中添加android:id =“ @ + id / transac_recycler_view”时,此方法就起作用了
答案 18 :(得分:0)
我认为你Adapter
中的问题。
确保您已在ViewHolder
中返回onCreateViewHolder()
。
如下所示:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_leaderboard, parent, false);
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}
答案 19 :(得分:0)
由于LinearLayoutManager默认是垂直的,因此更简单的方法是:
recyclerView.setLayoutManager(new LinearLayoutManager(context));
如果要更改方向,可以使用此构造函数:
public LinearLayoutManager(Context context, int orientation, boolean reverseLayout);
答案 20 :(得分:-1)
使用片段
在onCreateView()中编写代码
RecyclerView recyVideo = view.findViewById(R.id.recyVideoFag);
使用活动
在onCreate()中编写代码 RecyclerView recyVideo = findViewById(R.id.recyVideoFag);
答案 21 :(得分:-1)
如果我在你那里,我会这样做。 onView(withId(android.R.id.list))。perform(RecyclerViewActions.scrollToPosition(3)); android.R.id.list将其更改为列表ID,位置将是您在数组中的位置。