android - Project runs on first time but when I run it again It shows project has Stopped -


as can tell question topic, when run program on emulator works on first time, when run again without changes, shows "(project name) has stop working.

i don't know how fix this, tried cleaning project, building apk again, still there no solution.

any please, appreciated.

this code:

public class mainactivity extends appcompatactivity { locationmanager locationmanager; locationlistener locationlistener;  textview latitude; textview longitude; textview accuracy; textview altitude; textview address;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     latitude = (textview) findviewbyid(r.id.latitude);     longitude = (textview) findviewbyid(r.id.longitude);     accuracy = (textview) findviewbyid(r.id.accuracy);     altitude = (textview) findviewbyid(r.id.altitude);       locationmanager = (locationmanager) this.getsystemservice(context.location_service);     // check if have location access permission     if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted) {          // mean don't have permissions, need ask it.         activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, 1); // 1 here sort of id identify request, called request code       } else {         // have permission          // 0 , 0 mean keep track of user location changes, can change every 10 seconds or that.         locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener);      }       locationlistener = new locationlistener() {         @override         public void onlocationchanged(location location) {               latitude.settext("latitude: " + location.getlatitude());             longitude.settext("longitude: " + location.getlongitude());             accuracy.settext("accuracy: " + location.getaccuracy());             altitude.settext("alitiude: " + location.getaltitude());            }          @override         public void onstatuschanged(string provider, int status, bundle extras) {          }          @override         public void onproviderenabled(string provider) {          }          @override         public void onproviderdisabled(string provider) {          }     }; }  @override // controls dialog asks user location access permission public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {     super.onrequestpermissionsresult(requestcode, permissions, grantresults);      // check if user clicked on dialog , choose allow.     if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) {          // if have permission, update user location. need check make code run!         if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) {              // 0 , 0 mean keep track of user location changes, can change every 10 seconds or that.             locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener);         }       }     } } 

even when copy code , put in new project, works again, same problem when run again doesnt work.

i got error

09-10 21:35:37.936 2588-2588/com.example.qubayel.test2 e/androidruntime: fatal exception: main process: com.example.qubayel.test2, pid: 2588 java.lang.runtimeexception: unable start activity componentinfo{com.example.qubayel.test2/com.example.qubayel.test2.mainactivity}: java.lang.illegalargumentexception: invalid listener: null @ android.app.activitythread.performlaunchactivity(activitythread.java:2665) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1477) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6119) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)

i found solution, problem i'm using locationlistener before defining it. on line of code:

 // 0 , 0 mean keep track of user location changes, can change every 10 seconds or that.     locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener); 

this definition should've been on top of locationlistener call:

 locationlistener = new locationlistener() {     @override     public void onlocationchanged(location location) {           latitude.settext("latitude: " + location.getlatitude());         longitude.settext("longitude: " + location.getlongitude());         accuracy.settext("accuracy: " + location.getaccuracy());         altitude.settext("alitiude: " + location.getaltitude());        }      @override     public void onstatuschanged(string provider, int status, bundle extras) {      }      @override     public void onproviderenabled(string provider) {      }      @override     public void onproviderdisabled(string provider) {      } }; 

final code should this:

public class mainactivity extends appcompatactivity { locationmanager locationmanager; locationlistener locationlistener;  textview latitude; textview longitude; textview accuracy; textview altitude; textview address;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     latitude = (textview) findviewbyid(r.id.latitude);     longitude = (textview) findviewbyid(r.id.longitude);     accuracy = (textview) findviewbyid(r.id.accuracy);     altitude = (textview) findviewbyid(r.id.altitude);       locationmanager = (locationmanager) this.getsystemservice(context.location_service);     locationlistener = new locationlistener() {         @override         public void onlocationchanged(location location) {               latitude.settext("latitude: " + location.getlatitude());             longitude.settext("longitude: " + location.getlongitude());             accuracy.settext("accuracy: " + location.getaccuracy());             altitude.settext("alitiude: " + location.getaltitude());            }          @override         public void onstatuschanged(string provider, int status, bundle extras) {          }          @override         public void onproviderenabled(string provider) {          }          @override         public void onproviderdisabled(string provider) {          }     };      // check if have location access permission     if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted) {          // mean don't have permissions, need ask it.         activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location}, 1); // 1 here sort of id identify request, called request code       } else {         // have permission          // 0 , 0 mean keep track of user location changes, can change every 10 seconds or that.         locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener);      } }   @override // controls dialog asks user location access permission public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) {     super.onrequestpermissionsresult(requestcode, permissions, grantresults);      // check if user clicked on dialog , choose allow.     if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) {          // if have permission, update user location. need check make code run!         if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted) {              // 0 , 0 mean keep track of user location changes, can change every 10 seconds or that.             locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistener);             }         }      } } 

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 -