简体   繁体   English

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

[英]App crashing when trying to use RecyclerView on android 5.0

I'm trying to mess with the new RecyclerView and whenever I try to run it, my app immediately crashes.我试图弄乱新的 RecyclerView,每当我尝试运行它时,我的应用程序都会立即崩溃。 It gives me NullPointerException for trying to access methods from android.support.v7.widget.RecyclerView .它给了我 NullPointerException 以尝试从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.我查看了其他帖子,发现大多数人都没有compile 'com.android.support:recyclerview-v7:+'但我试过了,它根本没有帮助。 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)这里是错误日志:(我会发布一张图片,但我还没有 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)

This issue usually occurs when no LayoutManager was provided for the RecyclerView .当没有为RecyclerView提供LayoutManager时,通常会发生此问题。 You can do it like so:你可以这样做:

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

In my case it was not connected to 'final', but to the issue mentioned in @NemanjaKovačević comment to @aga answer.就我而言,它与“最终”无关,而是与@NemanjaKovačević 对@aga 答案的评论中提到的问题有关。 I was setting a layoutManager on data load and that was the cause of the same crash.我在数据加载时设置了 layoutManager,这也是导致同样崩溃的原因。 After moving the layoutManager setup to onCreateView of my fragment the issue was fixed.将 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);

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.对我来说,我遇到了同样的问题,问题是 xml 中有一个未使用的 RecyclerView 视图消失了,但我没有将它绑定到 Activity 中的任何适配器,因此出现了问题。 It was solved as soon as I removed such unused recycler views in xml一旦我在 xml 中删除了这种未使用的回收器视图,它就解决了

ie - 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" />

I experienced this crash even though I had the RecyclerView.LayoutManager properly set.即使我正确设置了RecyclerView.LayoutManager我也遇到了这个崩溃。 I had to move the RecyclerView initialization code into the onViewCreated(...) callback to fix this issue.我不得不将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);
}

You need to use setLayoutManager in the RecyclerView#onCreate() method.您需要在RecyclerView#onCreate()方法中使用setLayoutManager Before adding recyclerView to a view, it must have the LayoutManager set.在将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));

}

I got this issue due to wrong reference of RecyclerView id.由于RecyclerView id 引用错误,我遇到了这个问题。

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

to

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

Check with you recycler view ID, pointing to actual recycler view solved my issue与您的回收商视图 ID 核对,指向实际的回收商视图解决了我的问题

My problem was in my XML lyout I have an android:animateLayoutChanges set 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:animateLayoutChanges from my layout and that resloved my problem.所以,我刚刚从我的布局中删除了android:animateLayoutChanges ,这解决了我的问题。

I had this problem when using Butterknife library.我在使用 Butterknife 库时遇到了这个问题。 I had:我有:

View rootView = inflater.inflate
                (R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(rootView);

But the correct version is:但正确的版本是:

View rootView = inflater.inflate
                (R.layout.fragment_recipe_detail_view, container, false);
ButterKnife.bind(this, rootView);

In Gradle implement this library :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'

In XML mention like this :XML 中这样提及:

<android.support.v7.widget.RecyclerView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/recyclerView"/>

In Fragment we use recyclerView like this :Fragment 中,我们像这样使用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;
    }

In Activity we use RecyclerView like this :Activity 中,我们像这样使用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);
   }

if you need horizontal RecyclerView do this :如果您需要水平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);

if you want to use GridLayoutManager using RecyclerView use like this :如果您想使用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);

I was using a Dependency Injection - ButterKnife and this problem arised because I had not binded the view.我正在使用依赖注入- ButterKnife 出现这个问题是因为我没有绑定视图。

So inside onCreate() I just inserted this line:所以在 onCreate() 我刚刚插入了这一行:

ButterKnife.bind(this);

My RecyclerView declaration with Dependency Injection:我的 RecyclerView 声明与依赖注入:

@BindView(R.id.recyclerview)
RecyclerView recyclerView;

For those who are using: implementation 'androidx.recyclerview:recyclerview:1.1.0-beta05'对于那些使用:实现 'androidx.recyclerview:recyclerview:1.1.0-beta05'

make sure you are using this in your xml layout确保您在 xml 布局中使用它

I also experienced a crash when running my app below API 27. Here's a link to the message在 API 27以下运行我的应用程序时,我也遇到了崩溃。这是消息的链接

Originally, I have created a style to change the font size of my textview inside the recyclerview that's why I created this code below: (I could have just called @dimen when changing the font size based on screen sizes)最初,我创建了一个样式来更改 recyclerview 中 textview 的字体大小,这就是我在下面创建此代码的原因:(我可以在根据屏幕大小更改字体大小时调用 @dimen)

Inside style.xml里面的style.xml

and added this in my theme并在我的主题中添加了这个

themes.xml主题文件

After investigating, I found out that this is the one causing me an error and after removing the style, I no longer experiencing a crash.经过调查,我发现这是导致我出错的原因,删除样式后,我不再遇到崩溃。

I tried all the possible solutions I found here in stack overflow and almost gave up on this, thankfully in my curiosity I thought why not removing the style maybe this causes the crash and then boom the crash is gone.我尝试了在堆栈溢出中找到的所有可能的解决方案,但几乎放弃了这一点,幸好出于好奇,我想为什么不删除样式可能会导致崩溃,然后崩溃就消失了。

Since LinearLayoutManager is vertical by default, an easier way to do this is:由于默认情况下 LinearLayoutManager 是垂直的,因此更简单的方法是:

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

I think the problem in your Adapter .我认为问题出在您的Adapter Make sure you have returned ViewHolder in onCreateViewHolder() .确保您已在onCreateViewHolder()返回ViewHolder Like below:像下面这样:

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

For me the problem was with my activity_main.xml(21) in which the recycleView didn't have an id.对我来说,问题出在我的 activity_main.xml(21) 中,其中 recycleView 没有 id。

activity_main.xml(21)活动_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活动_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"
    />

It worked when i added a android:id="@+id/transac_recycler_view" to the activity_main.xml(21) recycleView当我向 activity_main.xml(21) recycleView 添加一个 android:id="@+id/transac_recycler_view" 时它起作用了

In my case, I added only butterknife library and forget to add annotationProcessor.就我而言,我只添加了黄油刀库而忘记添加 annotationProcessor。 By adding below line to build.gradle (App module), solved my problem.通过将以下行添加到 build.gradle(App 模块),解决了我的问题。

    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

Recyclerview recylerview = findbyid(R.id.recyclerview) 可能你错过了

I had similar problem when I test my application on GenyMotion.我在 GenyMotion 上测试我的应用程序时遇到了类似的问题。 After restart the device in GenyMotion problem was solved在 GenyMotion 中重启设备后问题解决了

I faced the same issue cause I had the RecyclerView in another layout and not on activity_main.xml.我遇到了同样的问题,因为我在另一个布局中而不是在 activity_main.xml 中有 RecyclerView。

By searching on some sites I got something and felt like sharing here通过在一些网站上搜索,我得到了一些东西,并想在这里分享

  1. Add Id to Include tag in Activity_main.xml在 Activity_main.xml 中添加 Id 以包含标签

    <include id="includedRecyclerLayout" .... </include>

  2. call this layout before Searching view by ID in MainActivity.java在 MainActivity.java 中按 ID 搜索视图之前调用此布局

    `ConstraintLayout includedLayout = findViewById(R.id.inckudedRecyclerLayout); `ConstraintLayout includesLayout = findViewById(R.id.inckudedRecyclerLayout);

    RecyclerViewrecyclerview=includedLayout.findViewById(R.id.RECYCLER_VIEW_ID)`; RecyclerViewrecyclerview=includedLayout.findViewById(R.id.RECYCLER_VIEW_ID)`;

I had this problem when using Butterknife , I was using fragment我在使用 Butterknife 时遇到了这个问题,我使用的是 fragment

For Fragment, it should be ButterKnife.bind(this,view);对于 Fragment,应该是ButterKnife.bind(this,view);

For Activity ButterKnife.bind(this);对于活动ButterKnife.bind(this);

If I where you I will do this.如果我在哪里你我会这样做。 onView(withId(android.R.id.list)).perform(RecyclerViewActions.scrollToPosition(3)); onView(withId(android.R.id.list)).perform(RecyclerViewActions.scrollToPosition(3)); android.R.id.list change it to your list id and position will be your position inside your array. android.R.id.list 将其更改为您的列表 ID,位置将是您在数组中的位置。

using fragment使用片段

writing code in onCreateView()在 onCreateView() 中编写代码

RecyclerView recyVideo = view.findViewById(R.id.recyVideoFag); RecyclerView recyVideo = view.findViewById(R.id.recyVideoFag);

using activity使用活动

writing code in onCreate() RecyclerView recyVideo = findViewById(R.id.recyVideoFag);在 onCreate() RecyclerView recyVideo = findViewById(R.id.recyVideoFag) 中编写代码;

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

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