在为FacebookLogin使用kotlin时,“未解决的参考”背后的原因是什么?

时间:2016-07-24 05:48:17

标签: android android-studio facebook-login kotlin

当我尝试实现图片中附带的代码时,我一直收到“未解析的引用:FacebookCallback”错误。

enter image description here

我正按照此链接中的说明设置Facebook登录信息:https://developers.facebook.com/docs/facebook-login/android#addbutton

我是Kotlin的新手,但我看不出我在这里做错了什么。

编辑:

以下是我的gradle文件:

    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'

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

此外,这是我尝试执行的等效代码,但是用普通Java编写(工作正常):

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 个答案:

答案 0 :(得分:7)

在Kotlin中匿名实施FacebookCallback的正确方法如下:

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

    override fun onCancel() {
    }

    override fun onError(error: FacebookException) {
    }
}

Object Expressions and Declarations文档提供了所有必要的详细信息。