Android: Custom ListView has an error while simulating

1

I've implemented a custom, basic ListView, but it's giving the error:

  

java.lang.RuntimeException: Unable to start activity   ComponentInfo {goodeal.listview / goodeal.listview.MainActivity}:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.ListView.setAdapter (android.widget.ListAdapter) 'on a   null object reference

My Adapter:

public class AdapterPropostas extends BaseAdapter {

    private final List<Proposta> propostas;
    private final Activity act;

    public AdapterPropostas(List<Proposta> propostas, Activity act) {
        this.propostas = propostas;
        this.act = act;
    }

    @Override
    public int getCount() {
        return propostas.size();
    }

    @Override
    public Object getItem(int i) {
        return propostas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        View view = act.getLayoutInflater()
                .inflate(R.layout.layout_linha, viewGroup, false);
        Proposta proposta = propostas.get(i);

        //pegando as referências das Views
        TextView valor = (TextView)
                view.findViewById(R.id.valorProposta);
        TextView qualificacao = (TextView)
                view.findViewById(R.id.qualificacaoCandidato);
        ImageView imagem = (ImageView)
                view.findViewById(R.id.fotoCandidato);


        //populando as Views
        valor.setText(proposta.getValor());
        qualificacao.setText(proposta.getQualificacao());
        imagem.setImageResource(R.drawable.para_drawable);

        return view;
    }
}

My Activity:

public class MainActivity extends AppCompatActivity {

    AdapterPropostas adapter;
    ListView listaDePropostas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Proposta> propostas = todasPropostas();

        listaDePropostas = (ListView) findViewById(R.id.listView);

        //chamada da nossa implementação
        adapter = new AdapterPropostas(propostas, this);

        listaDePropostas.setAdapter(adapter);

    }

    private List<Proposta> todasPropostas(){

        List<Proposta> listaProp = new ArrayList<Proposta>();
        Proposta proposta = new Proposta();
        proposta.setValor("36,21");
        proposta.setQualificacao("5,0");

        listaProp.add(proposta);

        return listaProp;

    }
}

My ListView:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal"
        android:choiceMode="multipleChoice" />
</LinearLayout>

My Line:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/fotoCandidato"
        android:layout_width="100dp"
        android:layout_height="match_parent" />

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

        <TextView
            android:id="@+id/valorProposta"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Titulo"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/qualificacaoCandidato"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="descriçao"
            android:textSize="20dp" />

    </LinearLayout>

</LinearLayout>

If someone can help me with this nullPointer ...

    
asked by anonymous 03.08.2016 / 05:40

1 answer

0

Where do you have the activity_main layout? Is it the one that is referenced as "My ListView"?

Is that if the listview is not in your activity_main layout when you do

listaDePropostas = (ListView) findViewById(R.id.listView); 

It will give null. This is because the findViewById function will look up the listView in the layout you declared in setContentView and you will not find it.

    
04.08.2016 / 09:46