我用地图开发app并将应用程序发布到市场。 现在,我有2次崩溃&来自我的应用程序的两个用户的ANR。但对我来说,我的工作完美! 我不知道问题是什么?! :( 这是我的代码和他们得到的错误:
public class MapCurrentPlace2 implements LocationListener {
public Context context;
double latitude = 0;
double longitude = 0;
String placeName = "";
// then use below code
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
Location location = null;
boolean isGPSEnabled;
boolean isNetworkEnabled;
private boolean canGetLocation;
public MapCurrentPlace2(Context context) {
super();
this.context = context;
}
public Location getLocation() {
try {
LocationManager locationManager = (LocationManager) context.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 is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (android.location.LocationListener) this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (android.location.LocationListener) this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@Override
public void onLocationChanged(Location arg0) {
}
public double getLat(){
location = getLocation();
latitude = location.getLatitude();
return latitude;
}
public double getLng(){
location = getLocation();
longitude = location.getLatitude();
return latitude;
}
第一个用户错误:
Android版: Android 4.21 装置:Lg g2
java.lang.NullPointerException
at com.bi.workclock.tools.MapCurrentPlace.getLatitude(MapCurrentPlace.java:42)
at com.bi.workclock.MainActivity.startTimer(MainActivity.java:269)
at com.bi.workclock.MainActivity.onLongClick(MainActivity.java:786)
at android.view.View.performLongClick(View.java:4376)
at android.view.View$CheckForLongPress.run(View.java:17613)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5171)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)
和秒用户错误: Android版本: Android 4.31 设备: Galaxy S3(m0)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.bi.workclock.tools.MapCurrentPlace.getPlaceName(MapCurrentPlace.java:74)
at com.bi.workclock.MainActivity.startTimer(MainActivity.java:271)
at com.bi.workclock.MainActivity.onLongClick(MainActivity.java:786)
at android.view.View.performLongClick(View.java:4511)
at android.view.View$CheckForLongPress.run(View.java:18758)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
这是我的代码版本2 ... **这是一个好方法吗?!:**
public class MapCurrentPlace {
public Context context;
double latitude = 0;
double longitude = 0;
String placeName = "";
public MapCurrentPlace(Context context) {
super();
this.context = context;
}
public double getLatitude() throws IOException {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location != null){
latitude = location.getLatitude();
}
} catch (Exception e) {
e.printStackTrace();
}
return latitude;
}
public double getLongitude() throws IOException {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location != null){
longitude = location.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
}
return longitude;
}
public String getPlaceName() throws IOException {
try {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
latitude = location.getLatitude();
longitude = location.getLongitude();
//Get location info
Geocoder gc = new Geocoder(context);
if(location != null && gc !=null){
List<Address> list = gc.getFromLocation(latitude, longitude, 1);
if(list.size()>0){
String city = list.get(0).getLocality();
String street = list.get(0).getAddressLine(0);
placeName = city+", "+street+"";
}
}else{
placeName = "";
}
} catch (Exception e) {
e.printStackTrace();
placeName = "";
}
return placeName;
}
谢谢大家,我喜欢这个网站!
答案 0 :(得分:1)
由于您的Geocoder课程有时会返回null值,这就是您的应用崩溃的原因使用Google Place Api通过您的lat long查找地址并按照链接使用Using Google Places API
答案 1 :(得分:0)
getLastKnownLocation()
如果用户未打开地图或重新安装应用程序,它将返回用户的最后已知位置,它将不具有任何最后已知位置。所以它会导致Null指针异常。
尝试以下代码: -
class abc implements LocationListener // then use below code
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(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 is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get 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", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
答案 2 :(得分:0)
我认为您缺少一些错误检测代码行。 添加一个if来检查arraylist.length是否为&gt;这应该可以解决星系s3的问题。 对于g2另一个如果对于纬度而且经度以及检查值是否为空,那么执行您正在执行的代码。
你总是需要这些检查,所以你最终不会因为这种情况发生FC关闭和错误。