2017-03-19 11 views
0

私はこのエラーで他の投稿を見てきました.1つの解決方法のスペルが間違っていて、別の場所にある解決策が組み込まれました。this投稿それは私のアプリがクラッシュしないようにしますが、私は許可されていないので、アプリが終了する原因となります。私は安全のために細かい場所や粗い場所の両方のアクセス許可を追加原因:java.lang.SecurityException: "パッシブ"ロケーションプロバイダにはACCESS_FINE_LOCATIONのアクセス許可が必要です

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.turtl.decider"> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.hardware.location.gps"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

は、ここに私のマニフェストです。

ここでは、エラーの原因となったコードです:

private void getLocation() throws SecurityException { 
     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (isPermissionGranted("ACCESS_FINE_LOCATION",context)) { 
      loc = locationManager.getLastKnownLocation(locationProvider); 
     } else { 
      showAlert("Insufficent Permissions", "Please grant this app GPS permissions to continue."); 
     } 
    } 

そして私は私がにリンクされている溶液からコピーしたコード:私はネクサス6Pエミュレータ上でAndroidのAPIレベル24を使用してい

public static boolean isPermissionGranted(String permission, Context c) { 
    //int res = ContextCompat.checkSelfPermission(context, permission); 
    return (ContextCompat.checkSelfPermission(c, permission) == PackageManager.PERMISSION_GRANTED); 
} 

。私が言ったように、アクセス許可が与えられているかどうかのチェックは、アプリがその行をスキップしているためクラッシュしないようにしていますが、ただ終了しています。

同じ結果で私のパーソナルデバイス(API 23)で実行しました。どんな助けもありがとうございます。

答えて

0

バグ#1:isPermissionGranted("ACCESS_FINE_LOCATION",context)は正しい文字列ではないため、間違っています。適切な文字列を入力するには、isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION, context)を使用します。

バグ#2:<uses-permission android:name="android.hardware.location.gps"/><uses-feature android:name="android.hardware.location.gps"/>である必要があります。その名前によるハードウェア機能はありますが、その名前による許可がないためです。

また、Manifest.permission.ACCESS_FINE_LOCATIONをリクエストするには、最終的にrequestPermissions()に電話する必要があります。アプリにはそれがないためです。

+0

これは確かに助けになりましたが、私は、現在の場所の緯度と経度を '' null '私が「getLocation」関数の4行目の下に書いたshowAlert(String.valueOf(loc.getLatitude())、String.valueOf(loc.getLongitude()));これをヌルポインタと見なす理由は何ですか? – CS2016

+1

@ CS2016: "なぜこれをヌルポインタとして見ているのですか? - 確かに。 'getLastKnownLocation()'は 'null'を返しました。これは多くの時間に起こります。ヌルを返すためには、[文書化されている](https://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String))です。その方法は最適化として使用され、場所を取得するための主要な方法とはなりません。参照:https://developer.android.com/guide/topics/location/strategies.html – CommonsWare

関連する問題