Myviewholder with error

Asked

Viewed 111 times

0

I’m studying about Recyclerview but when I try to execute the code it has a strange error.

android.view.InflateException: Binary XML file line #43: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:761)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
  at br.com.danilo.concurso.adapter.ConcursoAdapter.onCreateViewHolder(ConcursoAdapter.java:32)
 at br.com.danilo.concurso.adapter.ConcursoAdapter.onCreateViewHolder(ConcursoAdapter.java:19)

I googled, but all I could find were generic answers. In case you need to see some file let me know

ps: I am implementing Recyclerview in a Fragment and its data is coming from a separate class that returns a List<Concurso>

Concursoadapter.java

package br.com.danilo.concurso.adapter;
import android.content.Context;
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 br.com.danilo.concurso.R;
import br.com.danilo.concurso.model.Concurso;
/**
  * Created by danil on 21/04/2017.
*/
public class ConcursoAdapter  extends RecyclerView.Adapter<ConcursoAdapter.MyViewHolder>{
private List<Concurso> mList;
private LayoutInflater mLayoutInflater;

public ConcursoAdapter(Context context, List<Concurso> concursos){
    mList = concursos;
    mLayoutInflater = (LayoutInflater)  context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
//CHAMANDO SÒ QUANDO SE TEM A NECESSIDADE DE CRIAR UMA NOVA VIEW
// estado de DURTIN
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //CONSTRUCAO PESADA
    View view = mLayoutInflater.inflate(R.layout.item_list, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}
//CHAMADO TODA HORA
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.titulo.setText(mList.get(position).getNome());
    holder.subtitulo.setText(mList.get(position).getSituacao());


}

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

public void addListItem(Concurso concurso, int position){
    mList.add(concurso);
    notifyItemInserted(position);

}
//CONSTRUCAO PESADA
public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView titulo;
    public TextView subtitulo;
    public MyViewHolder(View itemView) {
        super(itemView);

        titulo = (TextView) itemView.findViewById(R.id.text_titulo);
        subtitulo = (TextView) itemView.findViewById(R.id.text_subtitulo);
    }
}
}

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="20dp"
    android:paddingLeft="16dp">

<ImageView
    android:layout_alignParentLeft="true"
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="6dp"
    android:src="@drawable/ic_dashboard_black_24dp"/>
<TextView
    android:textColor="@color/colorPrimary"
    android:id="@+id/text_titulo"
    android:layout_toRightOf="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="16dp"
    android:text="Concurso IRPF 2017"
    android:textSize="16sp"/>

<TextView
    android:textColor="@color/colorPrimary"
    android:layout_below="@+id/text_titulo"
    android:layout_alignLeft="@+id/text_titulo"
    android:id="@+id/text_subtitulo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="16dp"
    android:text="40 vagas"
    android:textSize="12sp"/>
<ImageView
    android:id="@+id/situacao"
    android:layout_width="50dp"
    android:layout_height="12sp"
    android:src="@drawable/ic_notifications_black_24dp"
    android:layout_below="@+id/text_titulo"
    android:layout_toEndOf="@+id/text_subtitulo"
    android:layout_above="@+id/view_divisor" />
<view
    android:id="@+id/view_divisor"
    android:layout_marginTop="20dp"
    android:layout_alignLeft="@+id/image"
    android:layout_below="@+id/text_subtitulo"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="0.8dp" />

What I had to change in Layout for it to work

...<View
    android:id="@+id/view_divisor"
    android:layout_marginTop="2dp"
    android:layout_alignLeft="@+id/image"
    android:layout_below="@+id/image"
    android:background="@color/colorPrimary"
    android:layout_width="match_parent"
    android:layout_height="0.8dp" />...
  • 1

    Put the code of your Adapter, as there is no way to know what causes the error without seeing the code.

  • @Márciooliveira added the code

  • 1

    Check if there is an error in the layout item_list.xml.

  • 1

    As the friend above mentioned, the problem seems to be in the layout. Checks if the ID’s in the layout match what you used as parameter in Viewholder findViewById.

  • apparently mine item_list.xml Okay, I’ll put it in the post for you to evaluate.

  • running the debug I noticed that it hangs on the line * View view = mLayoutInflater.inflate(R.layout.item_list, Parent, false);? so it doesn’t even call the findviewbyid()

Show 1 more comment

1 answer

1


The error has nothing strange. Alias, is very enlightening.

It says that at line 43 of the layout, when trying to compare a null value, it finds an error. So it points to the location and describes the problem.

The question is, what appears to be a separator at the end of the layout.

Examining your code, I have only doubts about the line:

 android:layout_height="0.8dp" 

I don’t know if it’s possible to use less than 1dp

My suggestion: delete this view and be if it works. When it’s in order, put a tab, if you still find it necessary.

In this case, use DividerItemDecoration See on documentation

EDITING: as the AP itself realized (and I did not), the whole issue was the fact of view start with uppercase and the code be miniscula.

<view <-- ERRADO

<View <-- CORRETO
  • Thanks for helping me with the error, I let it go. I ended up changing to another view (I left in the post) but I will also read about what you said.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.