Java 尝试在 android 5.0 上使用 RecyclerView 时应用程序崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27416834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:20:29  来源:igfitidea点击:

App crashing when trying to use RecyclerView on android 5.0

javaandroidandroid-layoutandroid-recyclerviewandroid-5.0-lollipop

提问by Joe

I'm trying to mess with the new RecyclerView and whenever I try to run it, my app immediately crashes. It gives me NullPointerException for trying to access methods from android.support.v7.widget.RecyclerView. I've looked at other posts and saw that most people didn't have compile 'com.android.support:recyclerview-v7:+'but I tried that and it hasn't helped at all. Not really sure what to do at this point, any help would be appreciated. Here the error log: (I would post a picture but I don't have 10 rep yet)

我试图弄乱新的 RecyclerView,每当我尝试运行它时,我的应用程序都会立即崩溃。它给了我 NullPointerException 以尝试从android.support.v7.widget.RecyclerView. 我看过其他帖子,发现大多数人都没有,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)

采纳答案by aga

This issue usually occurs when no LayoutManagerwas provided for the RecyclerView. You can do it like so:

当没有LayoutManagerRecyclerView. 你可以这样做:

final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

回答by YuriK

In my case it was not connected to 'final', but to the issue mentioned in @NemanjaKova?evi? comment to @aga answer. I was setting a layoutManager on data load and that was the cause of the same crash. After moving the layoutManager setup to onCreateView of my fragment the issue was fixed.

就我而言,它与“最终”无关,而是与@NemanjaKova?evi 中提到的问题有关?评论@aga 答案。我在数据加载时设置了 layoutManager,这也是导致同样崩溃的原因。将 layoutManager 设置移动到我的片段的 onCreateView 后,问题得到解决。

Something like this:

像这样的东西:

@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);

回答by Shirish Kamath

I experienced this crash even though I had the RecyclerView.LayoutManagerproperly set. I had to move the RecyclerViewinitialization code into the onViewCreated(...)callback to fix this issue.

即使我的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);
}

回答by l-l

Since LinearLayoutManager is vertical by default, an easier way to do this is:

由于默认情况下 LinearLayoutManager 是垂直的,因此更简单的方法是:

recyclerView.setLayoutManager(new LinearLayoutManager(context));

recyclerView.setLayoutManager(new LinearLayoutManager(context));

If you want to change the orientation you could use this constructor:

如果你想改变方向,你可以使用这个构造函数:

public LinearLayoutManager(Context context, int orientation, boolean reverseLayout);

回答by Maurice

For me, I was having the same issue, the issue was that there was an unused RecyclerView in xml with view gone but I am not binding it to any adapter in Activity, hence the issue. It was solved as soon as I removed such unused recycler views in xml

对我来说,我遇到了同样的问题,问题是 xml 中有一个未使用的 RecyclerView 视图消失了,但我没有将它绑定到 Activity 中的任何适配器,因此出现了问题。一旦我在 xml 中删除了这种未使用的回收器视图,它就解决了

i.e - I removed this view as this was not called in code or any adapter has been set

即 - 我删除了这个视图,因为它没有在代码中调用,或者已经设置了任何适配器

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv_profileview_allactivities"
        android:visibility="gone" />

回答by katwal-Dipak

You need to use setLayoutManagerin the RecyclerView#onCreate()method. Before adding recyclerViewto a view, it must have the LayoutManagerset.

您需要setLayoutManagerRecyclerView#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));

}

回答by Steve

I got this issue due to wrong reference of RecyclerViewid.

由于RecyclerViewid引用错误,我遇到了这个问题。

recyclerView = (RecyclerView) findViewById(R.id.rv_followers_list);

to

recyclerView = (RecyclerView) findViewById(R.id.rv_search_list);

回答by iam_muqshid

recyclerView =
            (RecyclerView) findViewById(R.id.recycler_view2);

Check with you recycler view ID, pointing to actual recycler view solved my issue

与您的回收商视图 ID 核对,指向实际的回收商视图解决了我的问题

回答by Android Dev

I think the problem in your Adapter. Make sure you have returned ViewHolderin onCreateViewHolder(). Like below:

我认为问题出在您的Adapter. 请确保你已返回ViewHolderonCreateViewHolder()。像下面这样:

@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;
}

回答by Ahlem Jarrar

My problem was in my XML lyout I have an android:animateLayoutChangesset to true and I've called notifyDataSetChanged() on the RecyclerView's adapter in the Java code.

我的问题是在我的 XML lyout 我有一个android:animateLayoutChanges设置为 true 并且我在 Java 代码中的 RecyclerView 的适配器上调用了 notifyDataSetChanged() 。

So, I've just removed android:animateLayoutChangesfrom my layout and that resloved my problem.

所以,我刚刚从我的布局中删除了android:animateLayoutChanges,这解决了我的问题。