首页 > 解决方案 > getLastKnownLocation 导致我的应用程序崩溃

问题描述

尝试运行我的应用程序时遇到此问题。当我到达标记的代码行时它会崩溃。我确定问题存在,因为当我删除这些行时应用程序会运行。有人可以帮我解决这个错误吗?

私人无效getLocation(){

    //Check Permissions again

    if (ActivityCompat.checkSelfPermission(Reclamation.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Reclamation.this,

            Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this,new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    }
    else
    { 
        **Location LocationGps= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location LocationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location LocalPassive= locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);**


    
        if (LocationGps !=null)
        {
            double lat=LocationGps.getLatitude();
            double longi=LocationGps.getLongitude();

            latitude=String.valueOf(lat);
            longitude=String.valueOf(longi);

            //showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
        }
        else if (LocationNetwork !=null)
        {
            double lat=LocationNetwork.getLatitude();
            double longi=LocationNetwork.getLongitude();

            latitude=String.valueOf(lat);
            longitude=String.valueOf(longi);

            //showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
        }
        else if (LocationPassive !=null)
        {
            double lat=LocationPassive.getLatitude();
            double longi=LocationPassive.getLongitude();

            latitude=String.valueOf(lat);
            longitude=String.valueOf(longi);

            //showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
        }
        else
        {
            Toast.makeText(this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
        }

        Intent intent = new Intent(Reclamation.this,SetLocation.class);
        intent.putExtra("latt",latitude);
        intent.putExtra("lonn",longitude);
       // startActivityForResult(intent, 3); */
    }

}

您的帮助将不胜感激

标签: javaandroidandroid-studio

解决方案


推荐阅读