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(...);
?– ramaral
I do this in oncreate this.list = (Listview)findViewById(R.id.lvContacts);
– Ilgner de Oliveira
Post all error log for more information.
– ramaral
I put in question the log
– Ilgner de Oliveira
It is necessary to have access to more of your code, mainly
onCreate
andonResume
. Are you sure thatthis.lista
is notnull
?– ramaral
Look there on create I do the finfviewbyid
– Ilgner de Oliveira