user9373603
user9373603

Reputation:

Error in my class

I don't know why I'm getting this exception. In my chat App whenever I try to send message, my app crash.

See the error in Logcat below:

 android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class TextView
    at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
    at com.jimmytrivedi.lapitchat.MessageAdapter.onCreateViewHolder(MessageAdapter.java:25)
    at com.jimmytrivedi.lapitchat.MessageAdapter.onCreateViewHolder(MessageAdapter.java:14)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)

Below is my MessageAdapter.java class where I'm getting this error in this line:

View view = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.message_single_layout, parent, false);

package com.jimmytrivedi.lapitchat;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {

    private List<Messages> messagesList;

    public MessageAdapter(List<Messages> messagesList) {
        this.messagesList = messagesList;
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.message_single_layout, parent, false);

        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
        Messages messages = messagesList.get(position);
        holder.chatText.setText(messages.getMessage());
    }

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

  public class MessageViewHolder extends RecyclerView.ViewHolder {

        public TextView chatText;
        public CircleImageView chatImage;

        public MessageViewHolder(View itemView) {
            super(itemView);
            chatText = itemView.findViewById(R.id.chatText);
            chatImage = itemView.findViewById(R.id.changeImage);
        }

    }

}

message_single_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MessageSingleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/chatImage"
        android:layout_width="38dp"
        android:layout_height="38dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="45dp"
        android:layout_marginTop="42dp"
        android:src="@drawable/defaultimage" />

    <TextView
        android:id="@+id/chatText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="98dp"
        android:layout_marginTop="49dp"
        android:textSize="20dp"
        android:background="@drawable/message_text_background"
        android:text="Hello" />

</RelativeLayout>

Is (android:background="@drawable/message_text_background") cause the problem? This is in my drawable 24 directory with root element of shape.

In video, the guy made drawable directory and in drawable directory he made the new drawable file. But in my case I'm using latest version so drawable directory is also exist and when I click on drawable there is no option to create new drawable file, so I did in drawable 24.

Upvotes: 1

Views: 183

Answers (3)

Vishal Sharma
Vishal Sharma

Reputation: 1061

enter image description here

Please make another background XML file and save them in the lowest version as shown in attached pic. If you have again doubt please let me know I will again clarify you.

Upvotes: 0

Vishal Sharma
Vishal Sharma

Reputation: 1061

"my end message_text_background is in drawable 24"

your mobile SDK version is 24 or not.

if not then change text background

Upvotes: 0

Radesh
Radesh

Reputation: 13565

i think message_text_background is the problem.

remove below line and check

android:background="@drawable/message_text_background"

your drawable not exist in that version

go to your app directory app/src/main/res and create drawable folder and paste your message_text_background in to it

Upvotes: 2

Related Questions