无法覆盖 OnActivityResult 方法

Posted

技术标签:

【中文标题】无法覆盖 OnActivityResult 方法【英文标题】:Can't override OnActivityResult method 【发布时间】:2018-08-02 16:28:44 【问题描述】:

我不太擅长 android Studio。

我启动了一个 WebView 项目,如果您忘记了密码,我们可以在其中登录/注册并编辑您的密码。我想从 Facebook 添加登录。我实现了 Facebook SDK 并在我的开发人员应用程序页面中添加了包和哈希键。我按照文档进行操作,选择从我的网页登录用户,并从我的 javascriptInterface 使用logInWithReadPermissions 方法调用 Facebook SDK。

我得到结果并从我的网页返回结果。要获取这些数据,我必须重写我的Activity 的方法onActivityResult(从AppCompatActivity 扩展)。 Android Studio 说我无法覆盖 onActivityResult 方法,因为此方法不会从超类中覆盖。 此外,如果我删除@Override,则永远不会调用该方法。

我从一个正在运行的旧 kotlin 项目中复制了这段代码。我不明白为什么我现在收到此错误。

我的 MainActivity 类:

package fr.versus.versus;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.login.LoginManager;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.safetynet.SafetyNet;
import com.google.android.gms.safetynet.SafetyNetApi;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity 

    public WebView webview;

    @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        this.webview = findViewById(R.id.webview);

        this.webview.setWebViewClient(new WebViewClient() 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
                Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            
        );

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
            WebView.setWebContentsDebuggingEnabled(true);
        

        this.webview.addJavascriptInterface(new JsObject(this), "android");

        WebSettings settings = this.webview.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);

        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        CookieManager.setAcceptFileSchemeCookies(true);

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            cookieManager.setAcceptThirdPartyCookies(webview, true);
        

        printHashKey(this);

        this.webview.loadUrl("file:///android_asset/main.html");
    

    @Override
    protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) 
        CallbackManager callbackManager = CallbackManager.Factory.create();

        callbackManager.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("FACEBOOK",AccessToken.getCurrentAccessToken().getToken());

        callJavascript("FACEBOOK", "\"token\": \"" + AccessToken.getCurrentAccessToken().getToken() + "\",\"id\": " + AccessToken.getCurrentAccessToken().getUserId() + "");
    

    private void callJavascript(String returnType, String value) 
        final String returnTypeFormated = "\"" + returnType.replace("\"", "\\\"") + "\"";
        final String valueFormated = "\"" + value.replace("\"", "\\\"") + "\"";

        this.webview.post(new Runnable()
            @Override
            public void run() 

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) 
                    MainActivity.this.webview.evaluateJavascript("inter(" + returnTypeFormated + "," + valueFormated + ");",null);
                else
                    MainActivity.this.webview.loadUrl("javascript:inter(" + returnTypeFormated + "," + valueFormated + ");");
                
            
        );
    

    private class JsObject 

        private Context context;

        JsObject(Context ctx) 
            context = ctx;
        

        @JavascriptInterface
        void getFacebook() 
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends", "email", "user_birthday", "user_photos"));
        

        @JavascriptInterface
        void getReCapcha()
            SafetyNet.getClient(context).verifyWithRecaptcha("6LfQe2cUAAAAAFxXDxFxZlICH9TN8NV6cBJRcoNq")
                .addOnSuccessListener(MainActivity.this, new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() 
                    @Override
                    public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) 
                        if (!response.getTokenResult().isEmpty()) 
                            MainActivity.this.callJavascript("CAPCHA",response.getTokenResult());
                        
                    
                )
                .addOnFailureListener(MainActivity.this, new OnFailureListener() 
                    @Override
                    public void onFailure(@NonNull Exception e) 
                        if (e instanceof ApiException) 
                            ApiException apiException = (ApiException) e;
                            Log.d("CAPCHA", "Error message: " +
                                    CommonStatusCodes.getStatusCodeString(apiException.getStatusCode()));
                         else 
                            Log.d("CAPCHA", "Unknown type of error: " + e.getMessage());
                        
                    
                );
        
    

我的应用 Gradle 文件:

apply plugin: 'com.android.application'

android 
    compileSdkVersion 27
    defaultConfig 
        applicationId "fr.versus.versus"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    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.facebook.android:facebook-login:4.35.0'

    implementation 'com.google.android.gms:play-services-safetynet:15.0.1'

    implementation 'com.stripe:stripe-android:6.1.2'

为什么我不能覆盖这个方法?

【问题讨论】:

int 在你 onActivityResult() 参数中替换 Integer 【参考方案1】:

是因为后面的方法不存在。

@Override
protected void onActivityResult(Integer requestCode, Integer resultCode,Intent data) 
    ...

使用

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);
    ...

Android Activity

【讨论】:

你真好。大多数新手开发者都有问题,这些老人总是想用他们的观点来点击投票而不是帮助。

以上是关于无法覆盖 OnActivityResult 方法的主要内容,如果未能解决你的问题,请参考以下文章

如何从小部件收听已关闭的活动? (onActivityResult只在Activity中)

Android - 无法解析方法'onActivityResult(int,int,android.content.Intent)

Facebook SDK - 如何正确覆盖 onActivityResult()?

Facebook AccountKit 在主要活动上覆盖 onActivityResult

onActivityResult方法不执行,啥原因?

从片段捕获图像时从不调用onActivityResult [重复]