无法在Google地图上恢复活动空指针例外

时间:2015-08-21 06:25:31

标签: android google-maps

我在我的位置和目的地之间进行谷歌地图绘制路线。

我正在尝试使用我的当前位置,因为我移动时会出现空指针异常。

在下面的代码中,我指出了错误行。

堆栈跟踪:

08-21 01:38:23.040: E/AndroidRuntime(11787): FATAL EXCEPTION: main
08-21 01:38:23.040: E/AndroidRuntime(11787): java.lang.RuntimeException: Unable to resume activity {com.prashant.adesara.googlemapv2/com.prashant.adesara.googlemapv2.MainActivity}: java.lang.NullPointerException
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.os.Looper.loop(Looper.java:137)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at java.lang.reflect.Method.invoke(Method.java:525)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at dalvik.system.NativeStart.main(Native Method)
08-21 01:38:23.040: E/AndroidRuntime(11787): Caused by: java.lang.NullPointerException
08-21 01:38:23.040: E/AndroidRuntime(11787):    at com.prashant.adesara.googlemapv2.MainActivity.onResume(MainActivity.java:97)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.Activity.performResume(Activity.java:5211)
08-21 01:38:23.040: E/AndroidRuntime(11787):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
08-21 01:38:23.040: E/AndroidRuntime(11787):    ... 12 more

MainActivity.java:

double startLatitude;
double startLongitude;
GoogleApiClient mGoogleApiClient;

LatLng start;

LocationManager lm;
Location location;

protected void onResume() {
    super.onResume();

    if (Utils.isConnected(getApplicationContext())) {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
        location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  --->97th Line

        startLatitude = location.getLatitude();
        startLongitude = location.getLongitude();

        start = new LatLng(startLatitude, startLongitude);

        //  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

        final TextView txtDistance = (TextView) findViewById(R.id.txtSpeed);
        new Routing(getParent(), mMap, txtDistance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start, end);
    }
}

我不确定如何获取默认用户纬度和经度位置。这就是它抛出null异常的原因。

1 个答案:

答案 0 :(得分:2)

建议您在onCreate方法中查找所有观看次数。

同时初始化LocationManager

onCreate

中执行此操作
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    txtDistance = (TextView) findViewById(R.id.txtSpeed);
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);

并在onResume()

    if (Utils.isConnected(getApplicationContext())) {

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
        location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  

        startLatitude = location.getLatitude();
        startLongitude = location.getLongitude();

        start = new LatLng(startLatitude, startLongitude);

        //  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

        new Routing(getParent(), mMap, txtDistance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, start, end);
    }