由于缺少位置权限,应用程序在启动时崩溃

Posted

技术标签:

【中文标题】由于缺少位置权限,应用程序在启动时崩溃【英文标题】:Application crashes on launch because of missing location permission 【发布时间】:2018-09-14 15:50:56 【问题描述】:

由于缺少权限,我的应用程序在启动时崩溃。

我已经在 manifest.xml 中添加了这些缺失的权限,并且还在类中实现了运行时权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

这是我的日志猫。上次打开它时它只是工作,我没有改变任何东西。

可能是什么问题?

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.escaper.smoosh4340, PID: 29873
   java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.escaper.smoosh4340/com.example.escaper.smoosh4340.LocationWeather: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6119)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
       at android.os.Parcel.readException(Parcel.java:1684)
       at android.os.Parcel.readException(Parcel.java:1637)
       at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:725)
       at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1205)
       at com.example.escaper.smoosh4340.LocationWeather.onCreate(LocationWeather.java:64)
       at android.app.Activity.performCreate(Activity.java:6679)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)

这是我的java类

  private static final String APP_ID="1c163fcac8ac8ca256fd3119777bee59";
    Button button;
    TextView textView;
    public static final int MY_PERMISSION_REQUEST_LOCATION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_weather);

        button = (Button)findViewById(R.id.btnGetlc);

        textView = (TextView)findViewById(R.id.textlc);
        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(ContextCompat.checkSelfPermission(LocationWeather.this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                    if(ActivityCompat.shouldShowRequestPermissionRationale(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION))
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]Manifest.permission.ACCESS_COARSE_LOCATION, MY_PERMISSION_REQUEST_LOCATION);

                    else 
                        ActivityCompat.requestPermissions(LocationWeather.this,
                                new String[]Manifest.permission.ACCESS_COARSE_LOCATION, MY_PERMISSION_REQUEST_LOCATION);
                    
                else 
                    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    try
                        textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                    catch (Exception e)
                        e.printStackTrace();
                        Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                    
                
            
        );

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);









        String units = "metric";
        String url = String.format("http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s",
                location.getLatitude(), location.getLongitude(), units,APP_ID);

        TextView textView = (TextView) findViewById(R.id.textweat);
        new WeatherDetection(textView).execute(url);

    

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) 
        switch (requestCode)
            case MY_PERMISSION_REQUEST_LOCATION:
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    if(ContextCompat.checkSelfPermission(LocationWeather.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
                        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        try
                            textView.setText(yourLocation(location.getLatitude(), location.getLongitude()));
                        catch (Exception e)
                            e.printStackTrace();
                            Toast.makeText(LocationWeather.this, "NOT FOUND, TURN ON GPS", Toast.LENGTH_SHORT).show();
                        
                    
                else
                    Toast.makeText(LocationWeather.this, "No permission!", Toast.LENGTH_SHORT).show();
                
            
        
    

【问题讨论】:

您在哪个 Android 版本上运行此程序?请发布获取位置的代码(我假设使用 LocationManager.NETWORK_PROVIDER)。 您在授予权限之前正在使用 locationManager。删除 button.setOnclicklistener 方法下面的两行。并获得android.permission.ACCESS_FINE_LOCATION 权限。 参考这个***.com/questions/32083913/… 【参考方案1】:

看看你的异常

Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

表示你错过了权限

android.permission.ACCESS_FINE_LOCATION

将该权限添加到 menifest.xml 以及您的类代码。

【讨论】:

措辞很重要。 “或”表示只有一个就足够了【参考方案2】:

您在调用之前没有检查您的权限:

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

你只有在点击按钮后才检查权限,所以为时已晚。

【讨论】:

以上是关于由于缺少位置权限,应用程序在启动时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Cordova - 由于联系权限,Android 应用程序在启动时崩溃

尝试在服务启动但应用程序崩溃时调用位置权限

Android - 由于设置撤销了位置权限导致应用崩溃

应用程序因缺少权限请求而崩溃[重复]

从服务 Android M 请求位置权限

Android getSimOperator() 在缺少权限时崩溃