Android ViewPager Frame Listview Crash -
i trying make tablayout (viewpager) app 3 fragments. in 1 of tabs, want display json parsed custom list view. custom list view , tab layout works fine individually (codes tutorial). when combine these two, app crashes immediately, couldn't figure out going wrong. hope guys can help
thank sam_0829....you saved me
the problems 1. instead of mainactivity, getmessages must in fragment
view empty while calling onviewcreated, declared private view variable
private view v;
//then oncreate
view v = inflater.inflate(r.layout.message_tab, container, false);
and passed
onviewcreated public void onviewcreated(view v, @nullable bundle savedinstancestate) { super.onviewcreated(v, savedinstancestate); contactlist = new arraylist<>();
lv = (listview) v.findviewbyid(r.id.list); new getmessages().execute(); }
and life became awesome
thank yo sam taught me trouble shoot via logs...something never trusted before
message fragment (solved code) ->
package com.example.user.newton; public class messagetab extends fragment { private string tag = mainactivity.class.getsimplename(); private progressdialog pdialog; private listview lv; private view v; // url messages json private static string url = "http://myjson.com/17oyz5"; arraylist<hashmap<string, string>> contactlist; private mainactivity.sectionspageradapter msectionspageradapter; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.message_tab, container, false); return v; } private class getmessages extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(getactivity()); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { httphandler sh = new httphandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url); log.e(tag, "response url: " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node jsonarray messages = jsonobj.getjsonarray("messages"); // looping through messages (int = 0; < messages.length(); i++) { jsonobject c = messages.getjsonobject(i); string message= c.getstring("message"); string sender= c.getstring("sender"); string mtime= c.getstring("time"); hashmap<string, string> contact = new hashmap<>(); // adding each child node hashmap key => value contact.put("message", message); contact.put("sender", sender); contact.put("mtime", mtime); contactlist.add(contact); } } catch (final jsonexception e) { log.e(tag, "json parsing error: " + e.getmessage()); getactivity().runonuithread(new runnable() { @override public void run() { toast.maketext(getactivity(), "json parsing error: " + e.getmessage(), toast.length_long) .show(); } }); } } else { log.e(tag, "couldn't json server."); getactivity().runonuithread(new runnable() { @override public void run() { toast.maketext(getactivity(), "couldn't json server. check logcat possible errors!", toast.length_long) .show(); } }); } 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( getactivity(), contactlist, r.layout.custom_layout, new string[]{"message", "sender", "mtime"}, new int[]{r.id.name, r.id.email, r.id.mobile}); lv.setadapter(adapter); } } @override public void onviewcreated(view v, @nullable bundle savedinstancestate) { super.onviewcreated(v, savedinstancestate); contactlist = new arraylist<>(); lv = (listview) v.findviewbyid(r.id.list); new getmessages().execute(); } }
messagefragment layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
please change
pdialog = new progressdialog(getapplicationcontext());
to
pdialog = new progressdialog(mainactivity.this);
& try.
Comments
Post a Comment