有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在版本21及以上的设备上运行应用程序时出现java错误?

我正在使用goofence应用程序检测用户位置,但它不支持21岁及以上的设备。我如何在所有设备上使用它

主要活动。爪哇

     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                            if (location == null) {
                                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this);

用于接收用户位置并从中接收更新的代码
这是我的主要活动类,如果我添加权限,我也会出错
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

 Error:(100, 64) error: incompatible types: <anonymous ConnectionCallbacks> cannot be converted to Context.
after implementing permission i get error like this.

     Build:gradle(app)


    apply plugin: 'com.安卓.application'

        安卓 {
            compileSdkVersion 23
            buildToolsVersion "25.0.2"
            defaultConfig {
                applicationId "com.example.barathkumarjt.geo"
                minSdkVersion 14
                targetSdkVersion 23
                versionCode 1
                versionName "1.0"
                testInstrumentationRunner "安卓.support.test.runner.AndroidJUnitRunner"
            }
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-安卓.txt'), 'proguard-rules.pro'
                }
            }
        }

        dependencies {
            compile ('com.安卓.support:appcompat-v7:23.2.1'){ exclude module: 'support-v4' }
            compile fileTree(dir: 'libs', include: ['*.jar'])
            安卓TestCompile('com.安卓.support.test.espresso:espresso-core:2.2.2', {
                exclude group: 'com.安卓.support', module: 'support-annotations'
            })


            compile'com.安卓.support:design:23.1.1'
            compile'com.google.安卓.gms:play-services:8.4.0'
            testCompile 'junit:junit:4.12'
        }

        Manifest file

         <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
            package="com.example.barathkumarjt.geo">
            <uses-permission 安卓:name="安卓.permission.INTERNET" />
            <uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION" />
            <uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION" />

            <application
                安卓:allowBackup="true"
                安卓:icon="@mipmap/ic_launcher"
                安卓:label="@string/app_name"
                安卓:theme="@style/AppTheme.NoActionBar">
                <meta-data
                    安卓:name="com.google.安卓.gms.version"
                    安卓:value="@integer/google_play_services_version" />

                <activity
                    安卓:name=".MainActivity"
                    安卓:label="@string/app_name" >
                    <intent-filter>
                        <action 安卓:name="安卓.intent.action.MAIN" />

                        <category 安卓:name="安卓.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>

                <service 安卓:name=".GeofenceTransitionsIntentService"  安卓:exported="true" />
            </application>
        </manifest> 
    Errors

    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.example.barathkumarjt.geo, PID: 711
                      java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
                          at 安卓.os.Parcel.readException(Parcel.java:1683)
                          at 安卓.os.Parcel.readException(Parcel.java:1636)
                          at com.google.安卓.gms.location.internal.zzi$zza$zza.zza(Unknown Source)
                          at com.google.安卓.gms.location.internal.zzk.zza(Unknown Source)
                          at com.google.安卓.gms.location.internal.zzl.zza(Unknown Source)
                          at com.google.安卓.gms.location.internal.zzd$1.zza(Unknown Source)
                          at com.google.安卓.gms.location.internal.zzd$1.zza(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zza$zza.zzb(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzg.zza(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzg.zzb(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzl.zzb(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzj.zzb(Unknown Source)
                          at com.google.安卓.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
                          at com.example.barathkumarjt.geo.MainActivity$1.onConnected(MainActivity.java:97)
                          at com.google.安卓.gms.common.internal.zzk.zzk(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzj.zzi(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzh.zzpx(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzh.onConnected(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzl.onConnected(Unknown Source)
                          at com.google.安卓.gms.common.api.internal.zzc.onConnected(Unknown Source)
                          at com.google.安卓.gms.common.internal.zzj$zzg.zzqL(Unknown Source)
                          at com.google.安卓.gms.common.internal.zzj$zza.zzc(Unknown Source)
                          at com.google.安卓.gms.common.internal.zzj$zza.zzw(Unknown Source)
                          at com.google.安卓.gms.common.internal.zzj$zzc.zzqN(Unknown Source)
                          at com.google.安卓.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
                          at 安卓.os.Handler.dispatchMessage(Handler.java:102)
                          at 安卓.os.Looper.loop(Looper.java:154)
                          at 安卓.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:755)

        My app is running on the device Api level 4.4.4 and while running on devices 21 and above the app is opening and get closed suddenly and i want to implement this app that support all devices please help me to implement it.
        Thanks and help in advance! 

共 (1) 个答案

  1. # 1 楼答案

    问题是,您需要启用运行时权限以促进此类操作

    if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    
          ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                12000);
    
    }
    

    请阅读本页:https://developer.android.com/training/permissions/requesting.html 当应用程序变得可见时执行此检查