调用位置管理器时出现空指针异常

时间:2015-05-15 07:51:02

标签: android

我正在尝试在应用中使用位置管理器 当我尝试使用位置管理器的对象空指针异常被抛出这里是我的snipet

 public class MainActivity extends Activity {

LocationManager locationManager;
float[] dist;
double rest;
Long res ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String serviceString = MainActivity.this.LOCATION_SERVICE;
    locationManager= (LocationManager)getSystemService(serviceString);
    String provider = LocationManager.GPS_PROVIDER;
    final Location loc1=locationManager.getLastKnownLocation(provider);
    String l = Double.toString(loc1.getLatitude());
  //  Log.d("Message: ",l);
    String netprovider=LocationManager.NETWORK_PROVIDER;

    LocationListener my = new LocationListener() {
       // double lat1 = loc1.getLatitude();
        //double  lon1=loc1.getLongitude();

        @Override
        public void onLocationChanged(Location loc1) {
            /*Location.distanceBetween(lat1,lon1,loc1.getLatitude(),loc1.getLongitude(),dist);
                res+=(long)dist[0];*/
                Toast.makeText(MainActivity.this,"Location changed",Toast.LENGTH_LONG).show();
                Log.d("Message: ", "Location changed, " + loc1.getAccuracy() + " , " + loc1.getLatitude() + "," + loc1.getLongitude());

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    locationManager.requestLocationUpdates(provider, 1000, 1, my);
}

请帮我解决问题。

和logcat

Process: com.example.pragadeeswaran.distancecalculator, PID: 9468
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pragadeeswaran.distancecalculator/com.example.pragadeeswaran.distancecalculator.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5196)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

getLastKnownLocation()可以返回null,因此您应该检查它:

String l;
if(loc1 != null)
    l = Double.toString(loc1.getLatitude());

答案 1 :(得分:0)

尝试替换此行

locationManager= (LocationManager)getSystemService(serviceString);

这一个

locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);