Android: Listview custom this with error while simulating

Asked

Viewed 63 times

1

I implemented a basic custom Listview, but this 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 anyone can help me with this nullPointer...

  • On which line is the error?

  • 2

    Where do you have the layout activity_main? Is it the one that is referred to as "My Listview"? Is that if the listview is not in your layout activity_main when you do listaDePropostas = (ListView) findViewById(R.id.listView); he will give null.

  • Jorge B. , Thank you, that’s right. I didn’t know that listview had to be on my main_layout. How do I inform you that your answer was correct to give you points ? I don’t know...I’m new here...

  • Sergio already created an answer, you can mark it as correct. The listview has to be in the layout of your activity, what you do setContentView.

  • Sergio already created an answer, you can mark as correct (seen on the left side of the answer) and vote positively (up arrow). The listview has to be in the layout of your activity, which you do setContentView.

1 answer

1


Where do you have the layout activity_main? It’s the one that’s listed as "My Listview"?

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

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

it will give null. This is because the function findViewById will look for the listview in the layout you declared on setContentView and you won’t find her.

Browser other questions tagged

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