使用RecyclerView时如何解决NullPointerException?

问题描述 投票:-3回答:2

我在我的应用程序中使用了两个RecyclerView,一个在MainActivity中,另一个在另一个活动中。 MainActivity中的一个可以正常工作,但另一个具有NullPointerException,并且我不知道问题出在哪里。

这是第二个RecyclerView的代码段,其中logcat表示存在Null对象引用。我使用了与第一个RecyclerView相同的代码,一切正常。

错误消息表明此行存在问题:recyclerView.setLayoutManager(layoutManager);

并且在此行initRecyclerViewFriendOverView();

在这里我实现了第二个RecyclerView:

private void initRecyclerViewFriendOverView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        RecyclerView recyclerView = findViewById(R.id.recyclerViewFriendOverView);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerViewAdapterFriendOverView adpater = new RecyclerViewAdapterFriendOverView(mNames, mImageUrls, this);
        recyclerView.setAdapter(adpater);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_overview);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        init();
        initRecyclerViewFriendOverView();
        getImages();
    }

这里是使用第二个RecyclerView的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewFreundesUebersicht"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

编辑:

问题是我在onCreate方法中对其进行初始化之前调用了RecyclerView。由于此问题已得到解决,请不要对此问题投反对票,因为我目前已被禁止,并且正在尝试改善我的问题。

谢谢您的帮助!

android android-recyclerview nullpointerexception
2个回答
1
投票

编辑

您正在使用include语句在活动中包含另一个xml文件,findViewById()将无法从包含的布局中查找视图。给您的include语句一个ID,首先找到该包含的视图,然后在该视图中调用findViewById():

在活动布局文件中:

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

并且在您的代码中:

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

RecyclerView recyclerview = includedLayout.findViewById(R.id.RECYCLER_VIEW_ID);

如果在recyclerView.setLayoutManager(layoutManager);处显示错误,则表示您的[[recyclerView为空。然后查看初始化的位置:

RecyclerView recyclerView = findViewById(R.id.recyclerViewFreundesUebersicht);
由于两个原因,它可以为空:

    您没有将RecyclerView添加到XML
  • 您的R.id.recyclerViewFreundesUebersicht引用了另一个xml中的视图
  • 检查您的XML并确保将您的recyclerview添加到XML中,并且您在此处使用的ID与活动中使用的ID相同。

  • 1
    投票
    像这样初始化LinearLayoutManager

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator());

    © www.soinside.com 2019 - 2024. All rights reserved.