NullPointerException while executing ListView.setAdapter (Android)

1

I'm implementing an application where I need to display a list of Dogs (already registered in the DB) and allow clicking on an item in the list to display a new activy to edit the data.

The problem is that I'm getting a NullPointerException when I try to run the setAdapter. Using the debug I realized that during the whole run, my ListView that I created to get the layout reference remains null even after using findViewByID ... Can anyone help with this? Below is the Layout and Activity:

package br.fatec.projeto.passeiacao;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

public class VerCaesActivity extends Activity{

    ListView listaCaes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listaCaes = (ListView) findViewById(R.id.listaCaesId);

        CaoDAO dao = new CaoDAO (this);
        List <Cao> caes = dao.obterLista ();

        ArrayAdapter <Cao> adapter = new ArrayAdapter <Cao> (this, android.R.layout.simple_list_item_1, android.R.id.text1, caes);

        listaCaes.setAdapter(adapter);

        listaCaes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "Teste", Toast.LENGTH_LONG).show();
            }
        });
    }
}

And below the Layout of this Activity:

<ListView
    android:id="@+id/listaCaesId"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"></ListView>

Thanks in advance, if you can help!

    
asked by anonymous 03.11.2017 / 20:40

1 answer

0

setContentView(R.layout.NomeDoSeuLayout); is missing, Android can not find your list because it does not know which layout to look for.

    
08.11.2017 / 13:52