gpt4 book ai didi

java - 如何避免 LOCATION_SERVICE 的 NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 13:12:38 25 4
gpt4 key购买 nike

在我的应用程序中,如果我的 LOCATION_SERVICE 关闭,我将收到 NullPointerException。但我想要一个警报对话框,它会提示用户激活 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 跟踪:

03-29 23:52:34.682: E/AndroidRuntime(11716): FATAL EXCEPTION: main 03-29 23:52:34.682: E/AndroidRuntime(11716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bddevs.ddbllocator/com.bddevs.ddbllocator.DBBL_LOCATOR}: java.lang.NullPointerException 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.access$600(ActivityThread.java:134) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.os.Handler.dispatchMessage(Handler.java:99) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.os.Looper.loop(Looper.java:137) 03-29 23:52:34.682: E/AndroidRuntime(11716): at 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): Caused by: 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): at android.app.Activity.performCreate(Activity.java:5008) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 03-29 23:52:34.682: E/AndroidRuntime(11716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027) 03-29 23:52:34.682: E/AndroidRuntime(11716): ... 11 more

最佳答案

如果定位服务被禁用。 getLastKnownLocation() 将返回 null。

添加空检查

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

关于java - 如何避免 LOCATION_SERVICE 的 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com