java - Android application exists and display an error `Unfortunately, TestGPS has stopped` -


i'm new android , when googled issue couldn't find proper solution problem.

i'm trying develop location based android app can find user location automatically. code compiles without error in android studio manager, when click on show location button, application exists , display error unfortunately, testgps has stoppedenter image description here

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.lsdias.testgps" >      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name=".gpstracker"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>      <uses-permission android:name="android.permission.access_fine_location" />     <uses-permission android:name="android.permission.internet" />  </manifest> 

gpstracker.java

package com.example.lsdias.testgps;  import android.app.alertdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.provider.settings; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.util.log; import android.view.view; import android.widget.button; import android.widget.toast;  public class gpstracker extends actionbaractivity implements locationlistener {     button btnshowlocation;     private final context mcontext;      // flag gps status     boolean isgpsenabled = false;      // flag network status     boolean isnetworkenabled = false;      boolean cangetlocation = false;      location location; // location     double latitude; // latitude     double longitude; // longitude      // minimum distance change updates in meters     private static final long min_distance_change_for_updates = 10; // 10 meters      // minimum time between updates in milliseconds     private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute      // declaring location manager     protected locationmanager locationmanager;      public gpstracker()     {         mcontext = null;     }      public gpstracker(context mcontext)     {         this.mcontext = mcontext;         getlocation();     }      public location getlocation()     {         try         {             locationmanager = (locationmanager) mcontext                     .getsystemservice(location_service);              // getting gps status             isgpsenabled = locationmanager                     .isproviderenabled(locationmanager.gps_provider);              // getting network status             isnetworkenabled = locationmanager                     .isproviderenabled(locationmanager.network_provider);              if (!isgpsenabled && !isnetworkenabled)             {                 // no network provider enabled             }             else             {                 this.cangetlocation = true;                 // first location network provider                 if (isnetworkenabled)                 {                     locationmanager.requestlocationupdates(                             locationmanager.network_provider,                             min_time_bw_updates,                             min_distance_change_for_updates, this);                     log.d("network", "network");                     if (locationmanager != null)                     {                         location = locationmanager                                 .getlastknownlocation(locationmanager.network_provider);                         if (location != null)                         {                             latitude = location.getlatitude();                             longitude = location.getlongitude();                         }                     }                 }                 // if gps enabled lat/long using gps services                 if (isgpsenabled)                 {                     if (location == null)                     {                         locationmanager.requestlocationupdates(                                 locationmanager.gps_provider,                                 min_time_bw_updates,                                 min_distance_change_for_updates, this);                         log.d("gps enabled", "gps enabled");                         if (locationmanager != null)                         {                             location = locationmanager                                     .getlastknownlocation(locationmanager.gps_provider);                             if (location != null)                             {                                 latitude = location.getlatitude();                                 longitude = location.getlongitude();                             }                         }                     }                 }             }          }         catch (exception e)         {             e.printstacktrace();         }          return location;     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_gpstracker);          btnshowlocation = (button) findviewbyid(r.id.button_location);          // show location button click event         btnshowlocation.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 if(cangetlocation())                 {                     double latitude = getlatitude();                     double longitude = getlongitude();                      // \n new line                     toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show();                 }                 else                 {                     // can't location                     // gps or network not enabled                     // ask user enable gps/network in settings                     showsettingsalert();                 }             }         });     }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_gpstracker, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      @override     public void onlocationchanged(location location) {      }      @override     public void onstatuschanged(string provider, int status, bundle extras) {      }      @override     public void onproviderenabled(string provider) {      }      @override     public void onproviderdisabled(string provider) {      }      /**      * function latitude      * */     public double getlatitude(){         if(location != null){             latitude = location.getlatitude();         }          // return latitude         return latitude;     }      /**      * function longitude      * */     public double getlongitude(){         if(location != null){             longitude = location.getlongitude();         }          // return longitude         return longitude;     }      /**      * function check if best network provider      * @return boolean      * */     public boolean cangetlocation() {         return this.cangetlocation;     }      /**      * function show settings alert dialog      * */     public void showsettingsalert(){         alertdialog.builder alertdialog = new alertdialog.builder(mcontext);          // setting dialog title         alertdialog.settitle("gps settings");          // setting dialog message         alertdialog.setmessage("gps not enabled. want go settings menu?");          // setting icon dialog         //alertdialog.seticon(r.drawable.delete);          // on pressing settings button         alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog,int which) {                 intent intent = new intent(settings.action_location_source_settings);                 mcontext.startactivity(intent);             }         });          // on pressing cancel button         alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                 dialog.cancel();             }         });          // showing alert message         alertdialog.show();     }      /**      * stop using gps listener      * calling function stop using gps in app      * */     public void stopusinggps(){         if(locationmanager != null){             locationmanager.removeupdates(gpstracker.this);         }     } } 

logcat

09-02 12:47:51.902    1935-1935/com.example.lsdias.testgps i/art﹕ not late-enabling -xcheck:jni (already on) 09-02 12:47:52.384    1935-1958/com.example.lsdias.testgps d/openglrenderer﹕ render dirty regions requested: true 09-02 12:47:52.385    1935-1935/com.example.lsdias.testgps d/﹕ hostconnection::get() new host connection established 0xa6c40550, tid 1935 09-02 12:47:52.418    1935-1935/com.example.lsdias.testgps d/atlas﹕ validating map... 09-02 12:47:52.582    1935-1949/com.example.lsdias.testgps i/art﹕ background sticky concurrent mark sweep gc freed 4140(288kb) allocspace objects, 0(0b) los objects, 28% free, 810kb/1135kb, paused 61.733ms total 123.372ms 09-02 12:47:52.607    1935-1958/com.example.lsdias.testgps d/﹕ hostconnection::get() new host connection established 0xa6c406b0, tid 1958 09-02 12:47:52.675    1935-1958/com.example.lsdias.testgps i/openglrenderer﹕ initialized egl, version 1.4 09-02 12:47:52.697    1935-1958/com.example.lsdias.testgps d/openglrenderer﹕ enabling debug mode 0 09-02 12:47:52.764    1935-1958/com.example.lsdias.testgps w/egl_emulation﹕ eglsurfaceattrib not implemented 09-02 12:47:52.764    1935-1958/com.example.lsdias.testgps w/openglrenderer﹕ failed set egl_swap_behavior on surface 0xa6c1ae20, error=egl_success 09-02 12:47:53.478    1935-1958/com.example.lsdias.testgps w/egl_emulation﹕ eglsurfaceattrib not implemented 09-02 12:47:53.478    1935-1958/com.example.lsdias.testgps w/openglrenderer﹕ failed set egl_swap_behavior on surface 0xa6c1ae20, error=egl_success 09-02 12:47:58.675    1935-1935/com.example.lsdias.testgps d/androidruntime﹕ shutting down vm     --------- beginning of crash 09-02 12:47:58.675    1935-1935/com.example.lsdias.testgps e/androidruntime﹕ fatal exception: main     process: com.example.lsdias.testgps, pid: 1935     java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources$theme android.content.context.gettheme()' on null object reference             @ android.app.alertdialog.resolvedialogtheme(alertdialog.java:154)             @ android.app.alertdialog$builder.<init>(alertdialog.java:379)             @ com.example.lsdias.testgps.gpstracker.showsettingsalert(gpstracker.java:240)             @ com.example.lsdias.testgps.gpstracker$1.onclick(gpstracker.java:155)             @ android.view.view.performclick(view.java:4756)             @ android.view.view$performclick.run(view.java:19749)             @ android.os.handler.handlecallback(handler.java:739)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:135)             @ android.app.activitythread.main(activitythread.java:5221)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 

what's issue here?

the problem mcontext in gpstracker null, , creating alertdialog null context.
should not override activity's constructors. move getlocation(); oncreate() method


Comments