What is the reason behind “unresolved reference” when using kotlin for FacebookLogin?

只愿长相守 提交于 2021-01-27 20:00:27

问题


I keep getting a "unresolved reference: FacebookCallback" error when I am trying to implement the code attached in the picture.

I am trying to set up facebook login as instructed in this link: https://developers.facebook.com/docs/facebook-login/android#addbutton

I am new to Kotlin but I can't see what I'm doing wrong here.

EDIT:

Here are my gradle files:

    apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "yetti.yetti"
        minSdkVersion 23
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }
    dexOptions {
        javaMaxHeapSize "2048M"
    }
    compileOptions.incremental = false
}

kapt {
    generateStubs = true
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
    compile 'com.jakewharton:butterknife:8.2.1'
    compile 'com.facebook.android:facebook-android-sdk:4.14.0'
    compile 'com.google.firebase:firebase-core:9.2.1'
    compile 'com.google.firebase:firebase-database:9.2.1'
    compile 'com.google.firebase:firebase-auth:9.2.1'
    testCompile 'junit:junit:4.12'
    kapt 'com.jakewharton:butterknife-compiler:8.2.1'
}

// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'

and:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.0.3'

    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-alpha6'
        classpath 'com.google.gms:google-services:3.0.0'
        // the latest version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

futhermore, here is the equivalent code I am trying to execute but written in plain Java (which works just fine):

import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;

public class Actv extends AppCompatActivity {

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

        FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        };
    }
}

回答1:


A proper way to anonymously implement FacebookCallback in Kotlin is as follows:

val facebookCallback = object : FacebookCallback<LoginResult> {
    override fun onSuccess(loginResult: LoginResult) {
    }

    override fun onCancel() {
    }

    override fun onError(error: FacebookException) {
    }
}

Object Expressions and Declarations documentation provides all necesary details.



来源:https://stackoverflow.com/questions/38549134/what-is-the-reason-behind-unresolved-reference-when-using-kotlin-for-facebookl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!