java - populating alert dialog with list view,JSON -
i trying fill alert dialog son response how ever getting following error :
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference
i have inflated list view in oncreate
other posts suggest
i have included relevant xml
, java
code
<?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="match_parent" android:orientation="horizontal" > <checkedtextview android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="?listpreferreditemheightsmall" android:checkmark="?android:attr/listchoiceindicatorsingle" android:gravity="center_vertical" android:paddingleft="?listpreferreditempaddingleft" android:paddingright="?listpreferreditempaddingright" android:textappearance="?textappearancelistitemsmall" /> </linearlayout> <?xml version="1.0" encoding="utf-8"?> <relativelayout 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" android:background="@color/colorprimary" tools:context=".queue.queuestatusfragment"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listviewresp" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingtop="15dp" android:layout_centervertical="true" android:layout_alignparentstart="true" /> </relativelayout> public class queuestatusfragment extends fragment{ arraylist<hashmap<string, string>> oslist = new arraylist<hashmap<string, string>>(); private textview txtqueue; private textview txtcust; private textview txttime; private textview txtbranch; private string urlstring; private button btnleave; private int reasonid; listview list; public queuestatusfragment() {} @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_queuestatus, container, false); txtqueue = (textview) v.findviewbyid(r.id.txtqueue); txttime = (textview) v.findviewbyid(r.id.txttime); txtcust = (textview) v.findviewbyid(r.id.txtcust); txtbranch = (textview) v.findviewbyid(r.id.txtbranch); list=(listview)v.findviewbyid(r.id.listviewresp); btnleave = (button) v.findviewbyid(r.id.btnleave); sessionv globalvariable = (sessionv) getactivity().getapplicationcontext(); urlstring = "http://172.20.10.5:1012/easyq.svc/rest/queuedetails/" + globalvariable.getcustid(); new processjson().execute(urlstring); system.out.println("works"); btnleave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { alertdialog alertdialog = new alertdialog.builder( getactivity()).create(); alertdialog.settitle("alert"); alertdialog.setmessage("you leave queue, sure?"); alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { urlstring = "http://172.20.10.5:1012/easyq.svc/rest/reasons"; new getreasons().execute(urlstring); for(int = 0; < oslist.size();i++) { string id = oslist.get(i).get("id"); system.out.println(id); } } }); alertdialog.show(); // sessionv globalvariable = (sessionv) getactivity().getapplicationcontext(); //string urlstring1 = "http://172.20.10.5:1012/easyq.svc/rest/leavequeue"; // new jsonhandler().execute(urlstring1); //system.out.println("works"); } }); // inflate layout fragment return v; } @override public void onattach(activity activity) { super.onattach(activity); } @override public void ondetach() { super.ondetach(); } private class processjson extends asynctask<string, void, string> { protected string doinbackground(string... strings) { string stream; string urlstring = strings[0]; httpdatahandler hh = new httpdatahandler(); stream = hh.gethttpdata(urlstring); // return data specified url system.out.println(stream); return stream; } protected void onpostexecute(string stream) { if (stream != null) { string[] array = stream.split(","); string part1 = array[1]; string part2 = array[2]; string part3 = array[3]; string part4 = array[4]; string queueid = array[5]; sessionv globalvariable = (sessionv) getactivity().getapplicationcontext(); globalvariable.setqueueid(queueid); if (part1 != null && part2 != null && part3 != null) { int minutes = (int) integer.parseint(part1) / 60; txtbranch.append("branch name: " + part4); txtqueue.append("service name: " + part3); txtcust.append("queue position: " + part2); txttime.append("waiting time: " + minutes + " minutes"); } } } } private class getreasons extends asynctask<string, void, string>{ protected string doinbackground(string... strings) { string stream; string urlstring = strings[0]; httpdatahandler hh = new httpdatahandler(); stream = hh.gethttpdata(urlstring); // return data specified url system.out.println(stream); return stream; } protected void onpostexecute(string stream) { if (stream != null) { try { jsonobject object = new jsonobject(stream); jsonarray array = object.getjsonarray("reasonsresult"); for(int = 0 ; < array.length(); i++) { jsonobject reasonobj = array.getjsonobject(i); string id = reasonobj.getstring("reason_leaving_id"); string reason = reasonobj.getstring("description"); hashmap<string, string> map = new hashmap<string, string>(); map.put("description", reason); map.put("id", id); oslist.add(map); } alertdialog alertdialog = new alertdialog.builder( getactivity()).create(); alertdialog.settitle("alert"); alertdialog.setmessage("you leave queue, sure?"); for(int = 0; < oslist.size(); i++) { listadapter adapter = new simpleadapter(getactivity(), oslist, r.layout.dialog_list, new string[] { "description"}, new int[] { r.id.text1}); list.setadapter(adapter); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string branchid = oslist.get(+position).get("id"); final sessionv globalvariable = (sessionv) getactivity().getapplicationcontext(); globalvariable.setbranchid(branchid); fragment fragment = null; fragment = new joinfragment(); getactivity().getsupportfragmentmanager().begintransaction().replace(r.id.container_body, fragment).commit(); } }); } alertdialog.show(); } catch(jsonexception e) { e.printstacktrace(); } } } } private class jsonhandler extends asynctask<string, void, string> { protected string doinbackground(string... strings) { string stream; string urlstring = strings[0]; httpdatahandler hh = new httpdatahandler(); final sessionv globalvariable = (sessionv) getactivity().getapplicationcontext(); system.out.println("sssss" + globalvariable.getqueueid() + globalvariable.getcustid()); jsonobject sender = new jsonobject(); try { sender.put("queueid", globalvariable.getqueueid()); sender.put("customerid", globalvariable.getcustid()); //sender.put("reasonid",); } catch (jsonexception e) { e.printstacktrace(); } stream = hh.post(urlstring, sender); // return data specified url system.out.println(stream); return stream; } protected void onpostexecute(string stream) { if (stream != null) { if (stream.equals("\"successful\"")) { toast.maketext(getactivity().getapplicationcontext(), "successfully left queue", toast.length_long).show(); fragment fragment=null; fragment=new homefragment(); getactivity().getsupportfragmentmanager().begintransaction().replace(r.id.container_body, fragment).commit(); } else if (stream.equals("\"not exist\"")) { toast.maketext(getactivity().getapplicationcontext(), "unable leave queue", toast.length_long).show(); } else if (stream.equals("\"not exist\"")) { toast.maketext(getactivity().getapplicationcontext(), "not in queue", toast.length_long).show(); } } } } } <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/colorprimary" tools:context=".queue.queuestatusfragment" > <textview android:id="@+id/txtbranch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="8" android:layout_centerhorizontal="true" android:layout_centerinparent="false" android:text="hello" android:layout_margintop="100dp" android:gravity="center|left" android:textcolor="#ffffff" android:textcolorhint="#ffffff" /> <textview android:id="@+id/txtqueue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="8" android:text="hello1" android:layout_margintop="160dp" android:gravity="center|left" android:textcolor="#ffffff" android:layout_centerhorizontal="true" android:layout_centerinparent="false" android:textcolorhint="#ffffff" /> <textview android:id="@+id/txttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="8" android:text="hello2" android:gravity="center|left" android:layout_margintop="44dp" android:layout_centerinparent="false" android:textcolor="#ffffff" android:textcolorhint="#ffffff" android:layout_below="@+id/txtqueue" android:layout_alignstart="@+id/txtqueue" /> <textview android:id="@+id/txtcust" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="8" android:text="hello3" android:gravity="center|left" android:textcolor="#ffffff" android:layout_margintop="47dp" android:layout_centerinparent="false" android:textcolorhint="#ffffff" android:layout_below="@+id/txttime" android:layout_alignstart="@+id/txttime" /> <button android:id="@+id/btnleave" android:layout_width="312dp" android:layout_height="wrap_content" android:background="@color/coloraccent" android:gravity="center" android:text="leave queue" android:textcolor="#ffffff" android:textstyle="bold" android:layout_marginbottom="65dp" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" /> </relativelayout>
i think problem in fragment_queuestatus
there no listview. add previous layout. why it's not listview , when try set adapter it's thing set in null object. so, add listview
in fragment_queuestatus
layout. solve problem.
Comments
Post a Comment