-1

**I am new to android studio and java

I'm trying to bind a button to an activity that contains a recycler view and the adapter that comes with it. The problem is when I click on the button the activity crashes. I know the button is not the problem because I have a different activity that uses a button and goes to the activity.

The problem is in either the activity for the recycler view which is named EdwardActivity or the adapter and I can't seem to figure it out.

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.edu.pfrfitness, PID: 10520
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edu.pfrfitness/com.edu.pfrfitness.EdwardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
        at com.edu.pfrfitness.EdwardActivity.onCreate(EdwardActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 10520 SIG: 9

Activity for recycler(named Edward Activity):

package com.edu.pfrfitness;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class EdwardActivity extends AppCompatActivity {

    RecyclerView recyclerView;

    String s1[], s2[];
    int images [] = {R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black, R.drawable.black};


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_row);

            recyclerView = findViewById(R.id.recyclerView);

            s1 = getResources() .getStringArray(R.array.Navigation_List);
            s2 = getResources() .getStringArray(R.array.description);

            MyAdapter myAdapter = new MyAdapter(this, s1, s2, images);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(myAdapter);

            //call recycler
        }


        public void  Recycler(){

        }






    }

Adapter:

package com.edu.pfrfitness;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    String data1[], data2[];
    int images[];
    Context context;

    public MyAdapter(Context ct, String s1[], String s2[], int img[]) {
            context = ct;
            data1 = s1;
            data2 = s2;
            images = img;


    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        holder.edwardText1.setText(data1[position]);
        holder.edwardText2.setText(data2[position]);
        holder.edwardImageView.setImageResource(images[position]);
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView edwardText1, edwardText2;
        ImageView edwardImageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            edwardText1 = itemView.findViewById(R.id.edwardText1);
            edwardText2 = itemView.findViewById(R.id.edwardText2);
            edwardImageView = itemView.findViewById(R.id.edwardImageView);
        }
    }


}

My row so like how each row will look:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/edwardImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:srcCompat="@tools:sample/avatars" />

            <TextView
                android:id="@+id/edwardText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp"
                android:text="Navigation_List"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintStart_toEndOf="@+id/edwardImageView"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/edwardText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="description"
                android:textSize="16sp"
                app:layout_constraintStart_toStartOf="@+id/edwardText1"
                app:layout_constraintTop_toBottomOf="@+id/edwardText1" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
  • if any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – Thanasis M Feb 25 '21 at 12:10

1 Answers1

2

Error explanation: According to the error you are trying to call setLayoutManager function on a null object. So, your variable "recyclerView" is obviously null.

Reason: The reason is that in your activity you do this:

setContentView(R.layout.my_row);

and you use the file "my_row.xml" to create your Activity layout. But, in this layout there is no view with id recyclerView. So, when you do this:

recyclerView = findViewById(R.id.recyclerView);

then variable recyclerView is set to null.

Solution: I guess that by mistake you used file "my_row.xml" as the Activity's layout, since this layout was obviously created fot the adapter's items. You should instead use another layout for the Activity's layout by passing it in your setContent in Activity's onCreate.

Thanasis M
  • 1,144
  • 7
  • 22