android - Using of JSON Parsing in MainActivity send FATAL ERROR -
i've got error when want parse json:
04-06 13:39:13.517: w/dalvikvm(2582): threadid=1: thread exiting uncaught exception (group=0xa62f9288) 04-06 13:39:13.521: e/androidruntime(2582): fatal exception: main 04-06 13:39:13.521: e/androidruntime(2582): java.lang.nullpointerexception 04-06 13:39:13.521: e/androidruntime(2582): @ jsonparser.gettunes.onpostexecute(gettunes.java:207) 04-06 13:39:13.521: e/androidruntime(2582): @ jsonparser.gettunes.onpostexecute(gettunes.java:1) 04-06 13:39:13.521: e/androidruntime(2582): @ android.os.asynctask.finish(asynctask.java:631) 04-06 13:39:13.521: e/androidruntime(2582): @ android.os.asynctask.access$600(asynctask.java:177) 04-06 13:39:13.521: e/androidruntime(2582): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 04-06 13:39:13.521: e/androidruntime(2582): @ android.os.handler.dispatchmessage(handler.java:99) 04-06 13:39:13.521: e/androidruntime(2582): @ android.os.looper.loop(looper.java:137) 04-06 13:39:13.521: e/androidruntime(2582): @ android.app.activitythread.main(activitythread.java:4745) 04-06 13:39:13.521: e/androidruntime(2582): @ java.lang.reflect.method.invokenative(native method) 04-06 13:39:13.521: e/androidruntime(2582): @ java.lang.reflect.method.invoke(method.java:511) 04-06 13:39:13.521: e/androidruntime(2582): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786)
update: class here:
public class gettunes extends asynctask<void, void, void> { private activity activity; private arraylist<hashmap<string, string>> data; private string domain, url; private progressdialog pdialog; lazyadapter lazyadapter; listview list; private static final string tag_tone = "objects"; // ================================================================= // category information private static final string tag_cat = "category"; private static final string tag_cat_cnt = "cnt_tones"; private static final string tag_cat_id = "id"; private static final string tag_catid = "cat_id"; private static final string tag_cat_img = "image"; private static final string tag_cat_title = "title"; private static final string tag_cattitle = "cat_title"; private static final string tag_cat_res_uri = "resource_uri"; // ================================================================= tone // information private static final string tag_download = "cnt_download"; private static final string tag_like = "cnt_like"; private static final string tag_create_date = "create_time"; private static final string tag_url = "file_url"; private static final string tag_tone_id = "id"; private static final string tag_toneid = "tone_id"; private static final string tag_title = "title"; private static final string tag_tonetitle = "tone_title"; private static final string tag_res_uri = "resource_uri"; // ================================================================= user // information private static final string tag_user = "user"; private static final string tag_user_avatar = "avatar"; private static final string tag_user_id = "id"; private static final string tag_userid = "user_id"; private static final string tag_user_name = "name"; // constructor public gettunes(activity a, arraylist<hashmap<string, string>> d, string domain, string url) { activity = a; data = d; domain = domain; url = url; } jsonarray ringtunes = null; // ===================================================================== // onpreexecute @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(activity); pdialog.setmessage("wait..."); pdialog.setcancelable(false); pdialog.show(); log.d("phase1 ", "> " + "done"); } // ===================================================================== // ===================================================================== // doinbackground @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making resquest url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("phase2 ", "> " + "done"); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node ringtunes = jsonobj.getjsonarray(mainactivity.tag_tone); // loopin through tones (int = 0; < ringtunes.length(); i++) { jsonobject ring = ringtunes.getjsonobject(i); // category node json object jsonobject category = ring.getjsonobject(mainactivity.tag_cat); string cat_cnt = category.getstring(mainactivity.tag_cat_cnt); string cat_id = category.getstring(mainactivity.tag_cat_id); string cat_img = category.getstring(mainactivity.tag_cat_img); string cat_title = category.getstring(mainactivity.tag_cat_title); string cat_uri = category.getstring(mainactivity.tag_cat_res_uri); string tone_download = ring.getstring(mainactivity.tag_download); string tone_like = ring.getstring(mainactivity.tag_like); string tone_createdate = ring.getstring(mainactivity.tag_create_date); string tone_url = domain + ring.getstring(mainactivity.tag_url); string tone_id = ring.getstring(mainactivity.tag_tone_id); string tone_title = ring.getstring(mainactivity.tag_title); string tone_uri = ring.getstring(mainactivity.tag_res_uri); // user node json object jsonobject user = ring.getjsonobject(mainactivity.tag_user); string user_avatar = domain + user.getstring(mainactivity.tag_user_avatar); string user_id = user.getstring(mainactivity.tag_user_id); string user_name = user.getstring(mainactivity.tag_user_name); // tem hashmap each tone hashmap<string, string> tones = new hashmap<string, string>(); // adding each child node hash map key > value tones.put(tag_cat_cnt, cat_cnt); tones.put(tag_catid, cat_id); tones.put(tag_cat_img, cat_img); tones.put(tag_cattitle, cat_title); // ====================================== tones.put(tag_download, tone_download); tones.put(tag_like, tone_like); tones.put(tag_create_date, tone_createdate); tones.put(tag_url, tone_url); tones.put(tag_toneid, tone_id); tones.put(tag_tonetitle, tone_title); // ====================================== tones.put(tag_user_avatar, user_avatar); tones.put(tag_userid, user_id); tones.put(tag_user_name, user_name); set keys = tones.keyset(); (iterator iter = keys.iterator(); iter.hasnext();) { string key = (string) iter.next(); string value = (string) tones.get(key); log.d(key, value); } // adding tones list data.add(tones); log.d("phase3 ", "> " + "done"); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); toast.maketext(activity, "couldn't data url", toast.length_long).show(); } return null; } // ===================================================================== // ===================================================================== // onpostexecute @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); // updating parsed json data list view lazyadapter = new lazyadapter(activity, data); animationadapter animadapter = new swingbottominanimationadapter(lazyadapter); animadapter.setinitialdelaymillis(300); animadapter.setabslistview(list); list.setadapter(animadapter); } // ===================================================================== }
i want use calss in main activity , called this:
new gettunes(mainactivity.this, tuneslist, thumb, url).execute();
list
null. you're not initializing anywhere in code. line error is: list.setadapter(animadapter);
. initialize either creating dynamically or setting resource id , problem solved.
Comments
Post a Comment