Я разрабатываю приложение с картой и публикую приложение на рынок. сейчас, у меня есть 2 аварии & ANRs от двух пользователей моего приложения. но для меня я отлично работаю! и у меня нет подсказки, в чем проблема ?! :( вот мой код и ошибка они получают:Моя карта Google api v2 сбой приложений
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)
и sec пользовательская ошибка: Версия для 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)
Вот мой код version2 ... ** это хороший способ?! **
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;
}
Спасибо всем я люблю этот сайт!
я попробовать ваши предложить, но то, что мне нужно делать в строке: ocationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, это); –
@ user3453502 не понимаю, что вы хотите сказать? – duggu
Я скопировать и вставить код, но строку: cpMIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES "является (cpMIN_TIME_BW_UPDATES не может быть разрешен к переменному) –