Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的谷歌地图api v2应用程序崩溃_Android_Google Maps_Crash Reports - Fatal编程技术网

Android 我的谷歌地图api v2应用程序崩溃

Android 我的谷歌地图api v2应用程序崩溃,android,google-maps,crash-reports,Android,Google Maps,Crash Reports,我用地图开发应用程序并将其发布到市场。 现在,我的两个应用程序用户发生了两次崩溃&anr。但对我来说,我的工作很完美! 我一点也不知道问题出在哪里?! :( 这是我的代码和他们得到的错误: public class MapCurrentPlace2 implements LocationListener { public Context context; double latitude = 0; double longitude = 0; String placeName = ""; //

我用地图开发应用程序并将其发布到市场。 现在,我的两个应用程序用户发生了两次崩溃&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;
}
第一个用户错误:

安卓版本: 安卓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用户错误: 安卓版本: 安卓4.31 设备: 星系S3(m0)

这是我的代码版本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;
    }
公共类MapCurrentPlace{
公共语境;
双纬度=0;
双经度=0;
字符串placeName=“”;
公共MapCurrentPlace(上下文){
超级();
this.context=上下文;
}
公共双getLatitude()引发IOException{
试一试{
LocationManager LocationManager=(LocationManager)context.getSystemService(context.LOCATION\u服务);
标准=新标准();
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
纬度=位置。getLatitude();
}
}捕获(例外e){
e、 printStackTrace();
}
返回纬度;
}
public double getLongitude()引发IOException{
试一试{
LocationManager LocationManager=(LocationManager)context.getSystemService(context.LOCATION\u服务);
标准=新标准();
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
longitude=location.getLongitude();
}
}捕获(例外e){
e、 printStackTrace();
}
返回经度;
}
公共字符串getPlaceName()引发IOException{
试一试{
LocationManager LocationManager=(LocationManager)context.getSystemService(context.LOCATION\u服务);
标准=新标准();
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
纬度=位置。getLatitude();
longitude=location.getLongitude();
//获取位置信息
地理编码器gc=新地理编码器(上下文);
if(位置!=null&&gc!=null){
List List=gc.getFromLocation(纬度,经度,1);
如果(list.size()>0){
字符串city=list.get(0.getLocation();
String street=list.get(0).getAddressLine(0);
地名=城市+,“+街道+”;
}
}否则{
地名=”;
}
}捕获(例外e){
e、 printStackTrace();
地名=”;
}
返回地名;
}

谢谢大家我爱这个网站!

这个
getLastKnownLocation()
返回用户的最后一个已知位置,如果用户没有打开地图或重新安装应用程序,它将没有任何最后一个已知位置。因此它将导致空指针异常

请尝试以下代码:-

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;
}

我认为您缺少一些错误检测代码行。 添加if以检查arraylist.length是否大于0。这将解决galaxy s3问题。 对于g2,如果要检查纬度和经度的值是否为空,则执行您正在执行的代码


你总是需要这样的检查,这样你就不会在这种情况下出现FC关闭和错误。

因为你的Geocoder类有时返回空值,这就是你的应用程序崩溃的原因。使用Google place Api通过你的lat long查找地址,并按照链接如何使用我尝试了你的建议,但我需要什么d行中要做的事情:LocationManager.requestLocationUpdates(LocationManager.GPS_提供者,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);@user3453502不明白你想说什么?我复制并粘贴了你的代码,但行:cpMIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES”是(cpMIN_TIME_BW_UPDATES无法解析为变量)@user3453502查看更新的代码,或者您必须实现LocationListener!好的,谢谢,但我实现LocationListener。但是我需要在MIN_TIME_BW_UPDATES和MIN_DISTANCE_CHANGE_中写入什么值浮点值来进行更新?!
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;
}