java - Stuck on Android.os.Bundle with Null Object Reference -
i don't know why error happens. have checked code link 1 still doesn't give solution.
the ide gimme error :
java.lang.nullpointerexception: attempt invoke virtual method 'int android.os.bundle.getint(java.lang.string)' on null object reference @ com.gook.rebill.fragment.homefragment.onattach(homefragment.java:74)
i know it's null object reference, still don't cause of error.
please me.
this code :
public class homefragment extends fragment implements internetconnectionlistener, apihandler.apihandlerlistener { private static final string arg_section_number = "section_number"; private final int category_action = 1; private categoryselectioncallbacks mcallbacks; private arraylist<category> categorylist; private listview categorylistview; private string error = null; private internetconnectionlistener internetconnectionlistener; public homefragment() { } public static homefragment newinstance(int sectionnumber) { homefragment fragment = new homefragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } @override public void onattach(activity activity) { super.onattach(activity); ((homeactivity) activity).onsectionattached(getarguments().getint(arg_section_number)); try { mcallbacks = (categoryselectioncallbacks) activity; } catch (classcastexception e) { throw new classcastexception("activity must implement categoryselectioncallbacks."); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home, container, false); categorylistview = (listview) rootview.findviewbyid(r.id.categorylistview); return rootview; } @override public void onresume() { super.onresume(); if (utilmethods.isconnectedtointernet(getactivity())) { initcategorylist(); } else { internetconnectionlistener = (internetconnectionlistener) homefragment.this; shownointernetdialog(getactivity(), internetconnectionlistener, getresources().getstring(r.string.no_internet), getresources().getstring(r.string.no_internet_text), getresources().getstring(r.string.retry_string), getresources().getstring(r.string.exit_string), category_action); } } public class getcateglist extends asynctask<void, void, void>{ @override protected void doinbackground(void... params) { /** * json populating text file. make api call use apihandler class * * <code>apihandler apihandler = new apihandler(this, url_get_category);</code> <br> * <code>apihandler.doapirequest(apihandler.request_get);</code> <br> * * response in onsuccessresponse(string tag, string jsonstring) method * if successful api call has done. parsing following. */ url hp = null; try { hp = new url( getstring(r.string.liveurl) + "foodcategory.php"); log.d("url", "" + hp); urlconnection hpcon = hp.openconnection(); hpcon.connect(); inputstream input = hpcon.getinputstream(); bufferedreader r = new bufferedreader(new inputstreamreader(input)); string x = ""; x = r.readline(); string total = ""; while (x != null) { total += x; x = r.readline(); } log.d("ur1l", "" + total); jsonarray j = new jsonarray(total); log.d("url1", "" + j.length()); categorylist = new arraylist<category>(); (int = 0; < j.length(); i++) { category category = new category();// buat variabel category jsonobject obj; obj = j.getjsonobject(i); //sama sperti yang lama, cman ini lebih mempersingkat karena getjsonobject cm d tulis sekali aja disini category.setid(obj.getstring(jf_id)); category.settitle(obj.getstring(jf_title)); category.seticonurl(obj.getstring(jf_icon)); if (!textutils.isempty(obj.getstring(jf_background_image))) { category.setimageurl(obj.getstring(jf_background_image)); } log.d("url1",""+obj.getstring(jf_title)); categorylist.add(category); } getactivity().runonuithread(new runnable() { @override public void run() { categorylistview.setadapter(new categoryadapter(getactivity(), mcallbacks, categorylist)); } }); }catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); error = e.getmessage(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); error = e.getmessage(); } catch (jsonexception e) { // todo auto-generated catch block error = e.getmessage(); e.printstacktrace(); } catch (nullpointerexception e) { // todo: handle exception error = e.getmessage(); } return null; } } //! function populate category list private void initcategorylist() { new getcateglist().execute(); } @override public void onconnectionestablished(int code) { if (code == category_action) { initcategorylist(); } } @override public void onusercanceled(int code) { if (code == category_action) { getactivity().finish(); } } //! catch json response here @override public void onsuccessresponse(string tag, string jsonstring) { //! same parsing done in initcategorylist() } //! detect response error here @override public void onfailureresponse(string tag) { } //! callback interface listen homeactivity detect user click on category public static interface categoryselectioncallbacks { void oncategoryselected(string catid, string title); } }
here use fragment code, in homeactivity:
public void onsectionattached(int number) { switch (number) { case 1: mtitle = getstring(r.string.title_section1); break; case 2: mtitle = getstring(r.string.title_section2); break; case 3: mtitle = getstring(r.string.title_section3); break; } }
i think getarguments()
return null.
can see these example:
android fragment getarguments() returns null
fragment getarguments() returns null
Comments
Post a Comment