androidinflate-exception

android.view.InflateException: Binary XML file line #15: Error inflating class <unknown>


I'm getting this error when I try to make an intent to a new AppCompatActivity but only in older Android APIs like 16, 22 or 23. In the new ones like API 26, it works correctly.

This is the log:

android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at valientes.valientesappv2.Domain.Mensajes.MensajeriaAdapter.onCreateViewHolder(MensajeriaAdapter.java:37)
    at valientes.valientesappv2.Domain.Mensajes.MensajeriaAdapter.onCreateViewHolder(MensajeriaAdapter.java:25)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
    at android.view.View.layout(View.java:16630)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <u

The third line of this is where the exception's thrown:

@Override
public MensajeriaAdapter.MensajesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
    return new MensajeriaAdapter.MensajesViewHolder(v);
}

This is the layout:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:id="@+id/eTEsribirMensaje"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Escribe un mensaje..."
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/bTenviarMensaje"
            android:layout_toStartOf="@+id/bTenviarMensaje" />
        <Button
            android:id="@+id/bTenviarMensaje"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Enviar"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appBarLayout"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:navigationIcon="?attr/homeAsUpIndicator"
                >
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:id="@+id/ivFoto"
                        android:layout_width="45dp"
                        android:layout_height="45dp"
                        android:src="@mipmap/ic_launcher" />

                    <TextView
                        android:id="@+id/tvTitulo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:gravity="center"
                        android:text="Título"
                        android:textSize="25dp"
                        android:textColor="@color/colorWhite"/>
                </RelativeLayout>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvMensajes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@layout/card_view_mensajes"
            android:layout_below="@+id/appBarLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/eTEsribirMensaje">
        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>
</LinearLayout>

And this is the cardViews I'm using in the Recyclerview of the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5sp"
    android:paddingRight="5sp">
    <android.support.v7.widget.CardView
        android:id="@+id/cvMensaje"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/colorWhiteTransparent"
        card_view:cardElevation="0dp">
        <LinearLayout
            android:id="@+id/mensajeBG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/out_message_bg"
            android:orientation="vertical">
            <TextView
                android:id="@+id/msTexto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxWidth="240sp"
                android:text="hola, qué tal?" />
            <TextView
                android:id="@+id/msHora"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="12:00 pm"
                android:textAppearance="?android:attr/textAppearanceSmall"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

The Adapter class:

 public class MensajeriaAdapter extends RecyclerView.Adapter<MensajeriaAdapter.MensajesViewHolder> {

    private List<MensajeDeTexto> mensajeDeTextos;
    private Context context;

    public MensajeriaAdapter(List<MensajeDeTexto> mensajeDeTextos, Context context) {
        this.mensajeDeTextos = mensajeDeTextos;
        this.context = context;
    }

    @Override
    public MensajeriaAdapter.MensajesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
        return new MensajeriaAdapter.MensajesViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MensajeriaAdapter.MensajesViewHolder holder, int position) {

        RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) holder.cardView.getLayoutParams();
        FrameLayout.LayoutParams fl = (FrameLayout.LayoutParams) holder.mensajeBG.getLayoutParams();

        LinearLayout.LayoutParams llMensaje = (LinearLayout.LayoutParams) holder.TvMensaje.getLayoutParams();
        LinearLayout.LayoutParams llHora = (LinearLayout.LayoutParams) holder.TvHora.getLayoutParams();

        if(mensajeDeTextos.get(position).getTipoMensaje()==1){//EMISOR
            holder.mensajeBG.setBackgroundResource(R.drawable.in_message_bg);
            rl.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
            rl.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            llMensaje.gravity = Gravity.RIGHT;
            llHora.gravity = Gravity.RIGHT;
            fl.gravity = Gravity.RIGHT;
            holder.TvMensaje.setGravity(Gravity.RIGHT);
        }else if(mensajeDeTextos.get(position).getTipoMensaje()==2){//RECEPTOR
            holder.mensajeBG.setBackgroundResource(R.drawable.out_message_bg);
            rl.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
            rl.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            llMensaje.gravity = Gravity.LEFT;
            llHora.gravity = Gravity.LEFT;
            fl.gravity = Gravity.LEFT;
            holder.TvMensaje.setGravity(Gravity.LEFT);
        }

        holder.cardView.setLayoutParams(rl);
        holder.mensajeBG.setLayoutParams(fl);
        holder.TvMensaje.setLayoutParams(llMensaje);
        holder.TvHora.setLayoutParams(llHora);

        holder.TvMensaje.setText(mensajeDeTextos.get(position).getMensaje());
        holder.TvHora.setText(mensajeDeTextos.get(position).getHoraDelMensaje());
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) holder.cardView.getBackground().setAlpha(0);
        else holder.cardView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
    }

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

    static class MensajesViewHolder extends RecyclerView.ViewHolder{

        CardView cardView;
        LinearLayout mensajeBG;
        TextView TvMensaje;
        TextView TvHora;

        MensajesViewHolder(View itemView){
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.cvMensaje);
            mensajeBG = (LinearLayout) itemView.findViewById(R.id.mensajeBG);
            TvMensaje = (TextView) itemView.findViewById(R.id.msTexto);
            TvHora = (TextView) itemView.findViewById(R.id.msHora);
        }
    }
}

Anyone knows what could be the problem?


Solution

  • You already have a context in your adapter class. Then why you try to get context from parent view?

    Try this

    View v = LayoutInflater.from(context).inflate(R.layout.card_view_mensajes,parent,false);
        return new MensajeriaAdapter.MensajesViewHolder(v);
    

    Instead of

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
        return new MensajeriaAdapter.MensajesViewHolder(v);
    

    Another: Please try to use a solid color instead of using android:background="@drawable/out_message_bg" on layout background.