java - I am receiving NullPointerException error after changing an activity to a fragment -


this question has answer here:

below logcat error getting, json_string = getactivity().getintent().getextras().getstring( "json_data" );

 09-10 21:56:07.507 22129-22129/com.example.aids.a09application e/androidruntime: fatal exception: main                                                                                      process: com.example.aids.a09application, pid: 22129                                                                                      java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.os.bundle.getstring(java.lang.string)' on null object reference                                                                                          @ com.example.aids.a09application.displaylistview.oncreateview(displaylistview.java:31)                                                                                          @ android.support.v4.app.fragment.performcreateview(fragment.java:2192)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1299)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.movefragmenttoexpectedstate(fragmentmanager.java:1528)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1595)                                                                                          @ android.support.v4.app.backstackrecord.executeops(backstackrecord.java:758)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.executeops(fragmentmanager.java:2363)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.executeopstogether(fragmentmanager.java:2149)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.optimizeandexecuteops(fragmentmanager.java:2103)                                                                                          @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:2013)                                                                                          @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:710)                                                                                          @ android.os.handler.handlecallback(handler.java:751)                                                                                          @ android.os.handler.dispatchmessage(handler.java:95)                                                                                          @ android.os.looper.loop(looper.java:154)                                                                                          @ android.app.activitythread.main(activitythread.java:6692)                                                                                          @ java.lang.reflect.method.invoke(native method)                                                                                          @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1468)                                                                                          @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1358) 

this fragment below lead fragment displayinlistview, using fragment transaction found @ bottom of code. intent displayinlistview activity, have changed fragment, due navigation drawer.

public class standingslist extends fragment implements view.onclicklistener {     //make member variable views     button mbutton;     button mbutton1;     textview mresult;     string json_response;     string json_string;     fragmenttransaction fragmenttransaction;      progressdialog mprogressdialog;       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view view = inflater.inflate( r.layout.fragment_standings, container, false );           //get reference of views         mbutton = (button) view.findviewbyid( r.id.button );         mbutton1 = (button) view.findviewbyid( r.id.buttontwo );         mresult = (textview) view.findviewbyid( r.id.result );         mbutton1.setonclicklistener(this);          //when button clicked         mbutton.setonclicklistener( new view.onclicklistener() {             @override             public void onclick(view view) {                 //call getjsonresponse method , fetch response server                 new getjsonresponse().execute();             }         } );          return view;     }           public class getjsonresponse extends asynctask<void, void, string> {             string serverurl;              public getjsonresponse() {                 mprogressdialog = new progressdialog( getactivity() );                 mprogressdialog.setmessage( "please wait" );                 mprogressdialog.settitle( "processing" );                 mprogressdialog.setcancelable( false );             }              @override             protected void onpreexecute() {                  //set url have fetch json response                 // not add real ip.                 serverurl = "http://xx.xx.xx.xx/get_info.php";                 mprogressdialog.show();             }              @override             protected string doinbackground(void... params) {                 try {                     url url = new url( serverurl );                     httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection();                     inputstream inputstream = httpurlconnection.getinputstream();                     bufferedreader bufferedreader = new bufferedreader( new inputstreamreader( inputstream ) );                     stringbuilder stringbuilder = new stringbuilder();                      while ((json_response = bufferedreader.readline()) != null) {                          stringbuilder.append( json_response + "\n" );                      }                      inputstream.close();                     bufferedreader.close();                     httpurlconnection.disconnect();                      return stringbuilder.tostring().trim();                  } catch (malformedurlexception e) {                     log.e( tag, "malformedurlexception: " + e ); //print exception message log                 } catch (ioexception e) {                     log.e( tag, "ioexception: " + e ); //print exception message log                 }                 return null;             }              @override             protected void onprogressupdate(void... values) {                 super.onprogressupdate( values );             }              @override             protected void onpostexecute(string result) {                 //set result returned doinbackground() method result textview                 mresult.settext( result );                 mprogressdialog.dismiss();                 json_string = result;             }          }              public void onclick(view view){                  switch (view.getid()) {                     case r.id.buttontwo:                         fragmenttransaction = getfragmentmanager().begintransaction();                         fragmenttransaction.replace( main_container, new displaylistview());                         fragmenttransaction.commit();                         break;                  }               }         } 

this class error is, on line 31 - json_string = getactivity().getintent().getextras().getstring( "json_data" );

this error has been created since converted class fragment activity.

public class displaylistview extends fragment {      string json_string;     jsonobject jsonobject;     jsonarray jsonarray;     standingsadapter standingsadapter;     listview listview;       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view view = inflater.inflate( r.layout.display_list_view_layout, container, false );         listview= (listview) view.findviewbyid( r.id.listviewparse );         standingsadapter = new standingsadapter(getactivity(),r.layout.row_layout);         listview.setadapter( standingsadapter );         json_string = getactivity().getintent().getextras().getstring( "json_data" );         try {             jsonarray  = new jsonobject(json_string).getjsonarray("server_response");               int count = 0;              int driver_id, team_id, position, points;             string firstname, lastname;                while(count<jsonarray.length())             {                 jsonobject jo = jsonarray.getjsonobject(count);                 driver_id = jo.getint( "driver_id" );                 team_id =jo.getint( "team_id" );                 firstname = jo.getstring( "first_name" );                 lastname= jo.getstring( "last_name" );                 position= jo.getint( "position" );                 points = jo.getint( "points" );                  standings standings = new standings(driver_id, team_id, firstname, lastname, position, points );                 standingsadapter.add( standings );                 count++;             }          } catch (jsonexception e) {             e.printstacktrace();         }          return view;     }  } 

now, have replaced activity new fragment.so have pass data argument.you can mention in below code.

    bundle bundle = new bundle();     bundle.putstring(constants.string_json, json_string);     replacefragment(displaylistview.newinstance(bundle)); 

now,you have json_string in actual fragnent ad below.

  public static displaylistview newinstance(bundle bungle) {     displaylistview fragment = new displaylistview();     fragment.setarguments(bungle);     return fragment;} 

you can retrieve value arguments in oncreate method of fragment below

string json_string = getarguments().getstring(constants.string_json); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -