-3

I am using two RecyclerViews in my app, one in the MainActivity and one in another activity. The one in the MainActivity works fine but the other has a NullPointerException and I don't know where the problem here is.

Here is a code snippet of the second RecyclerView, where the logcat says that there is a Null object reference. I used the same code as the first RecyclerView where everything works.

The error message says that there is a problem in this line: recyclerView.setLayoutManager(layoutManager);

and in this line initRecyclerViewFriendOverView();

Here is where I implemented my second 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();
    }

Here ist the XML where the second RecyclerView is used:

<?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>

Edit:

The problem was that I called the RecyclerView before initializing it in the onCreate method.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kaiser
  • 606
  • 8
  • 22

2 Answers2

1

Initialize LinearLayoutManager like this

  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
mDeveloper
  • 122
  • 9
1

EDIT:

You are including another xml file in your activity using include statement, findViewById() won't be able to find views from included layouts. Give an id to your include statement, find that included view first and then call findViewById () in that view:

In Activity layout file:

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

And in your code:

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

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

If it shows error at recyclerView.setLayoutManager(layoutManager); it means your recyclerView is null. Then you look at where you initialized it:

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

It can be null because of two reasons:

  • You didn't add the RecyclerView to XML
  • Your R.id.recyclerViewFreundesUebersicht is referring to a view from another xml

Check your XML and make sure your recyclerview was added to XML and the id you are using here is same as the one used in activity.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29