简体   繁体   中英

Can't override OnActivityResult method

I am not really good with Android Studio.

I started a WebView project where we can log in / register and edit your password if you forget it. I wanted to add Log In from Facebook. I implemented the Facebook SDK and added the package & hash key in my developer application page. I followed the documentation and I chose to log the user in from my webpage and call the Facebook SDK with the logInWithReadPermissions method from my JavascriptInterface.

I get the result and return the result from my webpage. To get this data, I must override the method onActivityResult of my Activity (extended from AppCompatActivity ). Android Studio says that I can't override the onActivityResult method because this method does not override from the superclass. Moreover, if I remove the @Override , the method is never called.

I copied this code from an older kotlin project that was working. I don't understand why I get this error now.

My MainActivity Class:

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());
                        }
                    }
                });
        }
    }
}

My App Gradle file:

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'
}

Why can't I override this method?

Is because the method follow don't exist.

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

Use

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

Android Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM