Sunday, 15 June 2014

android - locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); return null pointer exception -



android - locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); return null pointer exception -

followind code homecoming null poiner exception location manager.

my gpstracker class private final context mcontext;

// flag gps status boolean isgpsenabled = false; // flag network status boolean isnetworkenabled = false; // flag gps status boolean cangetlocation = false; location location; // location double latitude; // latitude double longitude; // longitude // minimum distance alter 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 min // declaring location manager protected locationmanager locationmanager; //location locmanager = locationmanager.getlastknownlocation(locationmanager.gps_provider); public gpstracker(context context) { this.mcontext = context; getlocation(); } public location getlocation() { seek { locationmanager = (locationmanager) this.getsystemservice(context.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(); } } } } } } grab (exception e) { e.printstacktrace(); } homecoming location; } /** * stop using gps listener * calling function stop using gps in app * */ public void stopusinggps(){ if(locationmanager != null){ locationmanager.removeupdates(gpstracker.this); } } /** * function latitude * */ public double getlatitude(){ if(location != null){ latitude = location.getlatitude(); } // homecoming latitude homecoming latitude; } /** * function longitude * */ public double getlongitude(){ if(location != null){ longitude = location.getlongitude(); } // homecoming longitude homecoming longitude; } /** * function check gps/wifi enabled * @return boolean * */ public boolean cangetlocation() { homecoming this.cangetlocation; } /** * function show settings alert dialog * on pressing settings button lauch settings options * */ 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?"); // 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(); } @override public void onlocationchanged(location location) { this.location = location; } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public ibinder onbind(intent arg0) { homecoming null; }

i trying phone call

if(gps.isgpsenabled){ toast.maketext(getbasecontext(),"gps enabled", toast.length_short).show(); baseurl = "http://my.careerfolio.net"; } else{ gps.showsettingsalert(); }

it display settings alert rather toast text, help?

my log.

06-20 02:54:06.336: w/system.err(909): java.lang.nullpointerexception 06-20 02:54:06.416: w/system.err(909): @ android.content.contextwrapper.getsystemservice(contextwrapper.java:519) 06-20 02:54:06.416: w/system.err(909): @ com.incv.mobile.freejscf.gpstracker.getlocation(gpstracker.java:50) 06-20 02:54:06.446: w/system.err(909): @ com.incv.mobile.freejscf.gpstracker.<init>(gpstracker.java:44) 06-20 02:54:06.446: w/system.err(909): @ com.incv.mobile.freejscf.myapplication.<init>(myapplication.java:34) 06-20 02:54:06.446: w/system.err(909): @ java.lang.class.newinstanceimpl(native method) 06-20 02:54:06.456: w/system.err(909): @ java.lang.class.newinstance(class.java:1130) 06-20 02:54:06.486: w/system.err(909): @ android.app.instrumentation.newapplication(instrumentation.java:990) 06-20 02:54:06.486: w/system.err(909): @ android.app.instrumentation.newapplication(instrumentation.java:975) 06-20 02:54:06.486: w/system.err(909): @ android.app.loadedapk.makeapplication(loadedapk.java:504) 06-20 02:54:06.486: w/system.err(909): @ android.app.activitythread.handlebindapplication(activitythread.java:4417) 06-20 02:54:06.486: w/system.err(909): @ android.app.activitythread.access$1300(activitythread.java:141) 06-20 02:54:06.486: w/system.err(909): @ android.app.activitythread$h.handlemessage(activitythread.java:1316) 06-20 02:54:06.506: w/system.err(909): @ android.os.handler.dispatchmessage(handler.java:99) 06-20 02:54:06.506: w/system.err(909): @ android.os.looper.loop(looper.java:137) 06-20 02:54:06.516: w/system.err(909): @ android.app.activitythread.main(activitythread.java:5103) 06-20 02:54:06.516: w/system.err(909): @ java.lang.reflect.method.invokenative(native method) 06-20 02:54:06.516: w/system.err(909): @ java.lang.reflect.method.invoke(method.java:525) 06-20 02:54:06.546: w/system.err(909): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 06-20 02:54:06.546: w/system.err(909): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 06-20 02:54:06.546: w/system.err(909): @ dalvik.system.nativestart.main(native method)

you trying getting location manager in location class not in activity context.so alter location class instance this activity's context mcontext. alter this

locationmanager = (locationmanager) this.getsystemservice(context.location_service);

to

locationmanager = (locationmanager) mcontext.getsystemservice(context.location_service);

edit if getting location using gps need add together access_fine_location (which includes both access_fine_location , access_coarse_location ). if getting network-based location need add together internet permission too.

so,open androidmanifest.xml file , next changes.

<uses-permission android:name="android.permission.access_fine_location" />    <uses-permission android:name="android.permission.internet" />  

android location

No comments:

Post a Comment