onRequestPermissionsResult方法不会从其超类重写方法吗?

时间:2019-03-19 04:32:12

标签: java android

onRequestPermissionsResultActivity.class中似乎没有可以实现的AppCompatActivity.class方法。

enter image description here

MainActivity.java

package com.example.ccccccc;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    }
}

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    defaultConfig {
        applicationId "com.example.ccccccc"
        minSdkVersion 22
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:22.2.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:22.2.0'
    implementation 'com.google.zxing:core:3.3.0'
    implementation 'com.google.zxing:javase:3.3.0'
}

3 个答案:

答案 0 :(得分:2)

首先onRequestPermissionsResult仅可从支持库24.1.0 under ActivityCompat.OnRequestPermissionsResultCallback.

中获得

因此,您需要用最少的build.gradle来修改targetSdkVersion 24,然后将implementation 'com.android.support:appcompat-v7:24.1.0'添加到您的依赖项中。

第二,这是不正确的:

@Override
protected void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

}

这是正确的:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

}

请注意,您需要public访问修饰符而不是protected

答案 1 :(得分:1)

该方法已添加到版本24.1的支持库中。您正在使用版本22。

答案 2 :(得分:1)

由于24.1.0以后可以使用onRequestPermissionsResult方法,请更新您的支持库> 24.1.0

所以您必须更新您的 build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.ccccccc"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.zxing:core:3.3.0'
    implementation 'com.google.zxing:javase:3.3.0'
}