“未解析的参考"背后的原因是什么?在将kotlin用于Facebook登录时? [英] What is the reason behind "unresolved reference" when using kotlin for FacebookLogin?

查看:110
本文介绍了“未解析的参考"背后的原因是什么?在将kotlin用于Facebook登录时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试实现图片中附带的代码时,我一直收到未解决的参考:FacebookCallback"错误.

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

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

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

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

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

这是我的gradle文件:

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'

和:

// 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编写(效果很好):

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) {

            }
        };
    }
}

推荐答案

匿名实现 FacebookCallback 如下:

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.

这篇关于“未解析的参考"背后的原因是什么?在将kotlin用于Facebook登录时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆