如何避免LOCATION_SERVICE的NullPointerException?

时间:2014-03-29 17:41:51

标签: java android eclipse google-maps nullpointerexception

在我的应用程序中,如果我的LOCATION_SERVICE关闭,我将收到NullPointerException。但是我想要一个alertDialog来提示用户激活LOCATION_SERVICE。任何人都可以告诉我如何处理这个例外?

 package com.bddevs.ddbllocator;
   import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends ActionBarActivity {
private GoogleMap googleMap;;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();

}

private void setUpMapIfNeeded() {
if (googleMap == null) {
    googleMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
    if (googleMap != null) {
        setUpMap();
    }
}

}

private void setUpMap() {
googleMap.setMyLocationEnabled(true);
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = LocationManager.NETWORK_PROVIDER;

Location myLocation = locationmanager.getLastKnownLocation(provider);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
    return true;
}
return super.onOptionsItemSelected(item);

}

这是LOGCAT TRACE:

  

03-29 23:52:34.682:E / AndroidRuntime(11716):致命异常:主要   03-29 23:52:34.682:E / AndroidRuntime(11716):java.lang.RuntimeException:无法启动活动ComponentInfo {com.bddevs.ddbllocator / com.bddevs.ddbllocator.DBBL_LOCATOR}:java.lang.NullPointerException   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread.access $ 600(ActivityThread.java:134)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1199)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.os.Handler.dispatchMessage(Handler.java:99)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.os.Looper.loop(Looper.java:137)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread.main(ActivityThread.java:4744)   03-29 23:52:34.682:E / AndroidRuntime(11716):at java.lang.reflect.Method.invokeNative(Native Method)   03-29 23:52:34.682:E / AndroidRuntime(11716):at java.lang.reflect.Method.invoke(Method.java:511)   03-29 23:52:34.682:E / AndroidRuntime(11716):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)   03-29 23:52:34.682:E / AndroidRuntime(11716):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)   03-29 23:52:34.682:E / AndroidRuntime(11716):at dalvik.system.NativeStart.main(Native Method)   03-29 23:52:34.682:E / AndroidRuntime(11716):引起:java.lang.NullPointerException   03-29 23:52:34.682:E / AndroidRuntime(11716):at com.bddevs.ddbllocator.DBBL_LOCATOR.setUpMap(DBBL_LOCATOR.java:73)   03-29 23:52:34.682:E / AndroidRuntime(11716):at com.bddevs.ddbllocator.DBBL_LOCATOR.setUpMapIfNeeded(DBBL_LOCATOR.java:38)   03-29 23:52:34.682:E / AndroidRuntime(11716):at com.bddevs.ddbllocator.DBBL_LOCATOR.onCreate(DBBL_LOCATOR.java:29)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.Activity.performCreate(Activity.java:5008)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)   03-29 23:52:34.682:E / AndroidRuntime(11716):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)   03-29 23:52:34.682:E / AndroidRuntime(11716):... 11更多

1 个答案:

答案 0 :(得分:1)

如果禁用了位置服务。 getLastKnownLocation()将返回null。

添加空检查

     Location myLocation = locationmanager.getLastKnownLocation(provider);
    If(myLocation == null ){
         // show the alert dialog
    }
   else {
         // do your stuff with latitude and longitude
  }