简体   繁体   中英

Android App crashing due to RecyclerView?

My main activity file

package com.example.custom_listapp;

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

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<row_data>arrContacts;
    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recyclerContact);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        arrContacts.add(new row_data("Christopher","9876513450",R.drawable.peter));
        arrContacts.add(new row_data("Rudraksh","9876513450",R.drawable.avatar2));
        arrContacts.add(new row_data("Keshav","9873403450",R.drawable.e));
        arrContacts.add(new row_data("Bilal","9876523450",R.drawable.d));
        arrContacts.add(new row_data("Niris","9876513450",R.drawable.h));
        arrContacts.add(new row_data("Ronaldo","9876513450",R.drawable.g));
        arrContacts.add(new row_data("Chris","9876513450",R.drawable.f));
        arrContacts.add(new row_data("Christopher","9876513450",R.drawable.trevor));
        arrContacts.add(new row_data("Christopher","9876513450",R.drawable.peter));
        arrContacts.add(new row_data("Rudraksh","9876513450",R.drawable.avatar2));
        arrContacts.add(new row_data("Keshav","9873403450",R.drawable.e));
        arrContacts.add(new row_data("Bilal","9876523450",R.drawable.d));
        arrContacts.add(new row_data("Niris","9876513450",R.drawable.h));
        arrContacts.add(new row_data("Ronaldo","9876513450",R.drawable.g));
        arrContacts.add(new row_data("Chris","9876513450",R.drawable.f));
        arrContacts.add(new row_data("Christopher","9876513450",R.drawable.trevor));
        arrContacts.add(new row_data("Christopher","9876513450",R.drawable.peter));
        arrContacts.add(new row_data("Rudraksh","9876513450",R.drawable.avatar2));
        arrContacts.add(new row_data("Keshav","9873403450",R.drawable.e));
        arrContacts.add(new row_data("Bilal","9876523450",R.drawable.d));
        arrContacts.add(new row_data("Niris","9876513450",R.drawable.h));
        RecyclerAdapter contactAdapter=new RecyclerAdapter(getApplicationContext(),arrContacts);
        recyclerView.setAdapter(contactAdapter);
    }
}

Activity main xml code

<?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="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerContact" />

</androidx.constraintlayout.widget.ConstraintLayout>

Customised design for list item xml code named as list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="7dp"
    app:cardUseCompatPadding="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/contactimg"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/trevor" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginLeft="11dp"
            android:layout_marginEnd="40dp"
            android:layout_marginRight="40dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtname"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="Christpher"
                android:textColor="#1D1111"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txtnumber"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="9373547390"
                android:textColor="#1D1111"
                android:textStyle="bold" />

        </LinearLayout>

    </LinearLayout>
</androidx.cardview.widget.CardView>

</LinearLayout>

** Row data java class to set type of arraylist to store three types of data**

package com.example.custom_listapp;

public class row_data {
    String name,number;
    int image;
    public row_data(String name,String number,int image){
        this.image=image;
        this.name=name;
        this.number=number;
    }
}

**Recycler adapter class **

package com.example.custom_listapp;

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;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Viewholder> {
    ArrayList<row_data> arrContacts;
    Context context;
    public RecyclerAdapter(Context context, ArrayList<row_data> arrContacts){
        this.context=context;
        this.arrContacts=arrContacts;
    }

    @NotNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(context).inflate(R.layout.list_row,parent,false);
        return new Viewholder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull  Viewholder holder, int position) {
    holder.img.setImageResource(arrContacts.get(position).image);
    holder.txtnumber.setText(arrContacts.get(position).number);
    holder.txtname.setText(arrContacts.get(position).name);
    }

    @Override
    public int getItemCount() {
        
        return arrContacts.size();
    }


    public  class Viewholder extends RecyclerView.ViewHolder{
      TextView txtname,txtnumber;
      ImageView img;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            this.txtname=itemView.findViewById(R.id.txtname);
            this.txtnumber=itemView.findViewById(R.id.txtnumber);
            this.img=itemView.findViewById(R.id.contactimg);
        }

    }
}

Android manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.custom_listapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Custom_listApp">
        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Still my App is crashing like app has been stopped dialog appears. I don't know whats the problem.

Your arrContacts is probably null as per the code you've posted.
You've declared the variable as ArrayList<row_data>arrContacts; but have not initialised it & therefore it crashed the app when you tried adding elements into a null list.

Simply use: ArrayList<row_data> arrContacts = new ArrayList();
or first declare & initialise in onCreate like below:

public class MainActivity extends AppCompatActivity {
    ArrayList<row_data> arrContacts;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // other things...
        arrContacts = new ArrayList();
        arrContacts.add(...) // Add items here
    }
}


On a side note,
you also do not need a Context parameter in your RecyclerAdapter to inflate a layout in onCreateViewHolder . You can use the provided ViewGroup 's context like below:

@NotNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.list_row, parent, false);
    return new Viewholder(view);
}

Init arrContacts = newArrayList<row_data>(); before use it.

your forget to initialize your Arraylist:

arrContacts = new Arraylist<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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