java - "Null object" error when setting a adapter -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i'm getting error when set adapter. have listview id lv_cine
in view.
edit 1: added wrong codes same this.
edit 2: ok managed past first error, i'm getting similar one:
12-01 20:43:44.445 7617-7617/com.example.maria.maria e/androidruntime﹕ fatal exception: main process: com.example.maria.maria, pid: 7617 java.lang.nullpointerexception: attempt invoke interface method 'int java.util.list.size()' on null object reference @ android.widget.simpleadapter.getcount(simpleadapter.java:93) @ android.widget.listview.setadapter(listview.java:487) @ com.example.maria.maria.cinema$getcontacts.onpostexecute(cinema.java:146) @ com.example.maria.maria.cinema$getcontacts.onpostexecute(cinema.java:67) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
here activity (cinema.java):
public class cinema extends activity { private progressdialog pdialog; listview listview; private static string url = "http://viniciuscoelho.com/android/dados"; // json node nomes private static final string tag_categoria = "cinema"; private static final string tag_nome = "nome"; private static final string tag_data = "data"; private static final string tag_endereco = "endereco"; private static final string tag_link = "link"; private static final string tag_desc = "desc"; // contacts jsonarray jsonarray dados = null; // hashmap listview arraylist<hashmap<string, string>> listadados; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cinema); listview = (listview) findviewbyid(r.id.lv_cine); new getcontacts().execute(); } /** * async task class json making http call * */ private class getcontacts extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(cinema.this); pdialog.setmessage("carregando..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node dados = jsonobj.getjsonarray(tag_categoria); // looping through dados (int = 0; < dados.length(); i++) { jsonobject c = dados.getjsonobject(i); string nome = c.getstring(tag_nome); string data = c.getstring(tag_data); string endereco = c.getstring(tag_endereco); string link = c.getstring(tag_link); string desc = c.getstring(tag_desc); // tmp hashmap single contact hashmap<string, string> linha = new hashmap<string, string>(); // adding each child node hashmap key => value linha.put(tag_nome, nome); linha.put(tag_data, data); linha.put(tag_endereco, endereco); linha.put(tag_link, link); linha.put(tag_desc, desc); // adding contact contact list listadados.add(linha); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( cinema.this, listadados, r.layout.linha, new string[] { tag_nome, tag_data, tag_endereco, tag_link, tag_desc }, new int[] { r.id.tvnome, r.id.tvdata, r.id.tvendereco, r.id.tvlink, r.id.tvdescricao }); //relaciona os dados ao prĂ³prio listview listview.setadapter(adapter); } } }
and view (activity_cinema.xml):
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:padding="@dimen/activity_horizontal_margin" android:scrollbars="none" android:background="@color/pome"> <listview android:id="@+id/lv_cine" android:background="@color/pome" android:layout_width="fill_parent" android:scrollbars="none" android:layout_height="wrap_content" android:layout_below="@+id/logo_cine" android:clickable="false" style="@style/listas"> </listview> </relativelayout>
linha.xml:
<relativelayout android:layout_width="fill_parent" android:orientation="horizontal" android:id="@+id/linha" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:paddingtop="20dp" android:id="@+id/tvnome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="22sp" android:textcolor="@color/white" android:textstyle="bold" android:text="nome"/> <textview android:id="@+id/tvdata" android:textstyle="bold" android:paddingbottom="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:layout_below="@+id/tvnome" android:textsize="16sp" android:text="data" /> <textview android:id="@+id/tvendereco" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:layout_below="@+id/tvdata" android:text="endereco" android:textcolorlink="@color/white" android:textsize="14sp" android:autolink="all" /> <textview android:id="@+id/tvlink" android:paddingbottom="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:layout_below="@+id/tvendereco" android:text="link" android:textcolorlink="@color/lightyellow" android:textsize="14sp" android:autolink="all" /> <textview android:id="@+id/tvdescricao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:layout_below="@+id/tvlink" android:textsize="16sp" android:text="descricao" android:paddingbottom="20dp" /> </relativelayout>
one thing can see far:
you didn't initialise listadados value, can done here:
@override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(cinema.this); pdialog.setmessage("carregando..."); pdialog.setcancelable(false); pdialog.show(); listadados = new arraylist<hashmap<string, string>>(); }
Comments
Post a Comment