0

I Create Recyclerview on android studio use Kotlin programing language, when I run my code I Get Error. This is the Error I'm getting

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.its*******.*******, PID: 18742
    java.lang.RuntimeException: Unable to start activity ComponentInfo{******************************}: android.view.InflateException: Binary XML file line #26: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.its*******.*******.******@36b9430
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3191)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3328)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2054)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)
     Caused by: android.view.InflateException: Binary XML file line #26: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.itsshyam640.cooltalks.userChat@36b9430
     Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{a9ab2d5 VFED..... ......I. 0,0-0,0 #7f080187 app:id/reyclerview_message_list}, adapter:null, layout:null, context:com.its*******.*******.*****@36b9430
        at androidx.recyclerview.widget.RecyclerView.generateLayoutParams(RecyclerView.java:4514)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:902)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:861)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:903)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:861)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.its*******.*******.*****.onCreate(userChat.java:35)
        at android.app.Activity.performCreate(Activity.java:7378)
        at android.app.Activity.performCreate(Activity.java:7369)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3171)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3328)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2054)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)

This is my mainfile code I had sent the required code only as per the requirements

public class userChat extends AppCompatActivity {
    private RecyclerView recyclerView_message_list;
    private ArrayList<userChatModel> userChatModels = new ArrayList<>();
    FirebaseDatabase database;
    FirebaseAuth auth;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_chat);
        getSupportActionBar().hide();

        database = FirebaseDatabase.getInstance();
        auth = FirebaseAuth.getInstance();


        String senderId = auth.getUid();
        String recievedId = getIntent().getStringExtra("userId");
        String userName = getIntent().getStringExtra("userName");
        String userProfileImage = getIntent().getStringExtra("userProfileImage");

        TextView username = findViewById(R.id.top_username);
        username.setText(userName);
        Picasso.get().load(userProfileImage).placeholder(R.drawable.user).into((ImageView) findViewById(R.id.user_profile));

        findViewById(R.id.back_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(userChat.this,MainActivity.class);
                startActivity(intent);
            }
        });


        recyclerView_message_list = (RecyclerView) findViewById(R.id.recyclerView_message_list);
        final userChatAdapter userChatAdapter = new userChatAdapter(userChatModels,this);
        recyclerView_message_list.setAdapter(userChatAdapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView_message_list.setLayoutManager(layoutManager);



        final String senderMessages = senderId + recievedId;
        final String recieverMessages = recievedId + senderId;

        database.getReference().child("Chats").child(senderMessages).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                userChatModels.clear();
                for(DataSnapshot snapshot1 : snapshot.getChildren()){
                    userChatModel userChatModel = snapshot1.getValue(userChatModel.class);
                    userChatModels.add(userChatModel);
                }
                userChatAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

Here is my 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"
    android:background="@color/backgroundColor"
    tools:context=".userChat">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/user_chat_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="30dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="16dp">


            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/back_button"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/round_gradient"
                android:src="@drawable/ic_keyboard_backspace_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/user_profile"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/profile_image"
                android:src="@drawable/user"
                app:layout_constraintBottom_toBottomOf="@id/back_button"
                app:layout_constraintStart_toEndOf="@id/back_button"
                app:layout_constraintTop_toTopOf="@id/back_button" />

            <TextView
                android:id="@+id/top_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginLeft="8dp"
                android:text="@string/username"
                android:textColor="@color/textColor"
                app:layout_constraintStart_toEndOf="@+id/user_profile"
                app:layout_constraintTop_toTopOf="@+id/user_profile" />

            <ImageView
                android:id="@+id/audioCall"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/audio_call_image"
                app:layout_constraintBottom_toBottomOf="@+id/videoCall"
                app:layout_constraintEnd_toStartOf="@+id/videoCall"
                app:layout_constraintTop_toTopOf="@+id/videoCall"
                app:srcCompat="@android:drawable/stat_sys_phone_call_forward" />

            <ImageView
                android:id="@+id/videoCall"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/video_call_image"
                android:src="@drawable/camera_pattern"
                app:layout_constraintBottom_toBottomOf="@+id/more_actions"
                app:layout_constraintEnd_toStartOf="@+id/more_actions"
                app:layout_constraintTop_toTopOf="@+id/more_actions" />

            <ImageView
                android:id="@+id/more_actions"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="5dp"
                android:contentDescription="@string/more_action_image"
                android:src="@drawable/ic_more_vert_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.appcompat.widget.Toolbar>

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

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

Here is my userChat Adapter Code

public class userChatAdapter extends RecyclerView.Adapter{

    ArrayList<userChatModel> messageModels;
    Context context;
    int SENDER_VIEW_TYPE = 1;
    int RECIEVER_VIEW_TYPE = 2;

    public userChatAdapter(ArrayList<userChatModel> messageModels, Context context) {
        this.messageModels = messageModels;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == SENDER_VIEW_TYPE){
            View view = LayoutInflater.from(context).inflate(R.layout.message_sent_layout,parent,false);
            return new senderViewHolder(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.message_recieved_layout,parent,false);
            return new recieverViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (messageModels.get(position).getUserId().equals(FirebaseAuth.getInstance().getCurrentUser())){
            return SENDER_VIEW_TYPE;
        }else {
            return RECIEVER_VIEW_TYPE;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        userChatModel messageModel = messageModels.get(position);

        if(holder.getClass() == senderViewHolder.class){
            ((senderViewHolder)holder).senderMessage.setText(messageModel.getUserMessage());
        }else {
            ((recieverViewHolder)holder).recieverMessage.setText(messageModel.getUserMessage());
        }
    }

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

    public class recieverViewHolder extends RecyclerView.ViewHolder{

        TextView recieverMessage, recieverTime;

        public recieverViewHolder(@NonNull View itemView) {
            super(itemView);

            recieverMessage = itemView.findViewById(R.id.user_recieved_message);
            recieverTime = itemView.findViewById(R.id.user_recieved_message_time);
        }
    }

    public class senderViewHolder extends RecyclerView.ViewHolder{

        TextView senderMessage, senderTime;
        public senderViewHolder(@NonNull View itemView) {
            super(itemView);

            senderMessage = itemView.findViewById(R.id.user_sent_message);
            senderTime = itemView.findViewById(R.id.user_sent_message_time);
        }
    }
}

I tried many ways but could not resolve the error.... I had applied many stackoverflow solutions.

Thanks in Advance!

  • Can you comment out the firebase code, so that the `RecyclerView` will show no data and tell if you get the same error – Zain Sep 13 '21 at 08:29

2 Answers2

0

Try to add

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerView_message_list"
          + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
     />

It determines if your layout is going to be Linear, Grid or Staggered. You can find more info about the different layoutManagers here: Android - What is Layout Manager?

And on the docs: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager

J.Stange
  • 478
  • 3
  • 8
0

Firstly remove these lines from your java file,

LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView_message_list.setLayoutManager(layoutManager);

Then add below XML snippet

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerView_message_list"
          + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
     />