Error while trying to fill a listview

Asked

Viewed 287 times

1

I’m trying to fill my listview like this

historicos=banco.getAllH(); 
ArrayAdapter<HistoricoObjeto> itens = new ArrayAdapter<HistoricoObjeto>(this, 
            android.R.layout.simple_list_item_1, historicos); 

this.lista.setAdapter(itens);
this.lista.setClickable(true);
registerForContextMenu(lista);

More is giving null Pointer Exception error on this line

this.lista.setAdapter(itens);

I’ll put the class Histoticoobject to be clearer what and the object I use

public class HistoricoObjeto implements Serializable{

    private String id;
    public String datahora;
    public String mensagem;

    public HistoricoObjeto() {

    }

    public HistoricoObjeto(String id, String datahora, String mensagem) {
        super();
        this.setId(id);
        this.setDatahora(datahora);
        this.setMensagem(mensagem); 
    }
}

Can anyone tell me where I’m going wrong?

11-25 12:22:02.809: E/AndroidRuntime(11457): FATAL EXCEPTION: main
11-25 12:22:02.809: E/AndroidRuntime(11457): java.lang.RuntimeException: Unable to resume activity {com.ilgner.chegouahora/com.ilgner.chegouahora.Historico}: java.lang.NullPointerException
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.os.Looper.loop(Looper.java:130)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at java.lang.reflect.Method.invoke(Method.java:507)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at dalvik.system.NativeStart.main(Native Method)
11-25 12:22:02.809: E/AndroidRuntime(11457): Caused by: java.lang.NullPointerException
11-25 12:22:02.809: E/AndroidRuntime(11457):    at com.ilgner.chegouahora.Historico.onResume(Historico.java:62)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.Activity.performResume(Activity.java:3832)
11-25 12:22:02.809: E/AndroidRuntime(11457):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
11-25 12:22:02.809: E/AndroidRuntime(11457):    ... 12 more




    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_historico);
    this.lista = (ListView)findViewById(R.id.lvContatos);
    this.banco = new Banco(getApplicationContext());
    HistoricoObjeto historico = new HistoricoObjeto();
    brCollator = Collator.getInstance(new Locale("pt", "BR"));
}

@Override
protected void onResume() {
    super.onResume();
    historicos=banco.getAllH();

    historicos=ordena(brCollator, historicos);
    ArrayAdapter<HistoricoObjeto> itens = new ArrayAdapter<HistoricoObjeto>(this, 
            android.R.layout.simple_list_item_1, historicos){ 
        @Override
        public View getView (int position, View convertView, ViewGroup parent) {

                View view = super.getView(position, convertView, parent);

                // Como o simple_list_item_1 retorna um TextView, esse cast pode ser feito sem problemas
                ((TextView) view).setTextColor(Color.BLACK);

                return view;
            }}; 

    Log.e("", ""+itens.getCount());
    this.lista.setAdapter(itens);
    this.lista.setClickable(true);
    registerForContextMenu(lista);

}

Strange stopped from this mistake there , and I didn’t even do anything , but I used this code there in another list but it was with Historico was with a class Person who had name , and phone there in the list appeared the name , more now not knowing how to make appear one of the variables in the list the history class has date and time and message wanted to make appear both or one

  • You’re not by chance forgetting to do this.lista = findViewById(...);?

  • I do this in oncreate this.list = (Listview)findViewById(R.id.lvContacts);

  • Post all error log for more information.

  • I put in question the log

  • It is necessary to have access to more of your code, mainly onCreate and onResume. Are you sure that this.lista is not null?

  • Look there on create I do the finfviewbyid

Show 1 more comment

1 answer

2


historicos=banco.getAllH(); this command it returns a list or an array?

Because if it is vector, it is coherent, because the last parameter of the Arrayadapter constructor must be a vector;

Nullpointexception type errors occur when the program attempts to access a memory object that has not been instantiated (or rather initialized) until the moment of its call. What does that mean? The historical is still null or it does not have a defined value.

I suggest you use Log to see if there’s any value within historical.

I hope I’ve helped!

  • He returns an Arraylist<Historicoobject>

  • analyze the following suspicions:

  • 1
    • see if you are adding correctly in the database, so that its return is different from Null. Another way to know if you have something in the database is to give a database.getsize(), which will return the size. * you are adding in an Arrayadapter an array of objects; you really need all the information inside that object or some specific one? Like... you just need the message, or the message, date and id? Because if you only need the message, create another array of the string type and assign these values!
  • 1

    I managed to solve the problem, thank you all very much

  • 1

    Solved my problem poses a toString

  • @Ilgner publish the solution you found and mark it as the accepted answer. In doing so it will be clear that you have already found a solution.

Show 1 more comment

Browser other questions tagged

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