我试图使用适用于Android和Groovy的GoogleDriveApi从Android访问Google云端硬盘帐户内容。但是我在logcat中遇到了一个严重错误,关于DriveApi类的ClassCastException,尽管我遵循了官方的Google教程,但使用了Groovy语言。 (为了简化问题,我创建了一个小项目,该项目再现了与原始项目相同的错误。)

这是构建gradle(项目是从命令行生成的:有一个唯一的gradle.build文件。提交此gradle构建是我以一种简单的方式为您提供依赖项的方法)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.10'
    }
}

apply plugin: 'android'
apply plugin: 'groovyx.grooid.groovy-android'

android {
    compileSdkVersion 'Google Inc.:Google APIs:23'
    buildToolsVersion '23.0.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}

repositories {
   jcenter()
}

project.androidGroovy {
    options {
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'
    }
}

dependencies {
    compile 'org.codehaus.groovy:groovy:2.4.6:grooid'
    compile 'com.arasthel:swissknife:1.4.0'
    compile 'com.google.android.gms:play-services-drive:8.4.0'
}


这是GoogleApiGroovyTest.groovy

package com.loloof64.android.google_api_test

import android.app.Activity
import android.os.Bundle
import android.content.IntentSender
import android.widget.Toast
import android.content.Context
import android.util.Log

import groovy.transform.CompileStatic

import com.arasthel.swissknife.dsl.components.GAsyncTask

import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.common.api.GoogleApiClient.Builder
import com.google.android.gms.drive.Drive
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GooglePlayServicesUtil
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener

import com.google.android.gms.drive.DriveFolder
import com.google.android.gms.drive.DriveApi
import com.google.android.gms.common.api.PendingResult

@CompileStatic
public class GoogleApiGroovyTest extends Activity
implements GoogleApiClient.ConnectionCallbacks,     GoogleApiClient.OnConnectionFailedListener
{

    private GoogleApiClient googleApiClient
    private final static int RESOLVE_CONNECTION_REQUEST_CODE = 4
    private final static String APP_LOGGER_REF = "GoogleApiGroovyTest"

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)

        googleApiClient = new     GoogleApiClient.Builder(this).addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build()
        googleApiClient.connect()
    }

    @Override
    public void onConnected(Bundle connectionHint){
        Toast.makeText(this, "Connected to Google Drive", Toast.LENGTH_SHORT).show()
        Context myThis = this
        async { context, GAsyncTask task ->
            task.after {
                Toast.makeText(myThis, "Finally printed items into screen", Toast.LENGTH_SHORT).show()
            }

            task.error { err ->
                Toast.makeText(this, "Error updating list files !", Toast.LENGTH_SHORT).show()
                Log.e(APP_LOGGER_REF, err.message)
                err.stackTrace.each { element ->
                    Log.e(APP_LOGGER_REF, element.toString())
                }
            }

            loadExplorerItems()

            true // Seem as returning a value is needed for the async task code to be executed
        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        //TODO
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult)     {
        // This block section is mandatory for GoogleApiClient to recover connection failure
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE)
            } catch (IntentSender.SendIntentException e) {
                // Unable to resolve
                Toast.makeText(this, "Failed to connect to Google account !", Toast.LENGTH_SHORT).show()
            }
        } else {
                GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show()
        }
        // End of block section
    }

    private void loadExplorerItems(){
        Context myThis = this
        DriveFolder appFolder = DriveApi.getAppFolder(googleApiClient)
        PendingResult<DriveApi.MetadataBufferResult> pendingChildren = appFolder.listChildren(googleApiClient)
        pendingChildren.setResultCallback     {DriveApi.MetadataBufferResult result -> processResult:{
            if (result.getStatus().isSuccess()){
                Toast.makeText(myThis, "Retrieved root content", Toast.LENGTH_SHORT).show()
            }
            else {
               Toast.makeText(myThis, "Failed to get root content", Toast.LENGTH_SHORT).show()
            }
        }}
    }
}


这是错误stacktrace:

04-08 16:58:53.549  3261  3261 E GoogleApiGroovyTest: Cannot cast object 'interface com.google.android.gms.drive.DriveApi' with class 'java.lang.Class' to class 'com.google.android.gms.drive.DriveApi'
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: com.loloof64.android.google_api_test.GoogleApiGroovyTest.loadExplorerItems(GoogleApiGroovyTest.groovy:91)
04-08 16:58:53.575  3261  3261 E GoogleApiGroovyTest: com.loloof64.android.google_api_test.GoogleApiGroovyTest.access$0(GoogleApiGroovyTest.groovy)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: com.loloof64.android.google_api_test.GoogleApiGroovyTest$_onConnected_closure1.doCall(GoogleApiGroovyTest.groovy:62)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: java.lang.reflect.Method.invoke(Native Method)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1021)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: groovy.lang.Closure.call(Closure.java:426)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: com.arasthel.swissknife.dsl.components.GAsyncTask.doInBackground(GAsyncTask.groovy:49)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: android.os.AsyncTask$2.call(AsyncTask.java:295)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-08 16:58:53.576  3261  3261 E GoogleApiGroovyTest: java.lang.Thread.run(Thread.java:818)


同时,DriveApi.getAppFolder方法具有以下签名:

abstract DriveFolder getAppFolder(GoogleApiClient apiClient)


DriveApi类和DriveFolder类都不采用类参数。所以我无法理解Stacktrace错误的真正含义。

Drive API Android reference

最佳答案

嗯,我不知道该错误来自何处,但是您试图像在类上调用静态方法一样尝试在接口上调用方法这一事实可能与它有关:

DriveFolder appFolder = DriveApi.getAppFolder(googleApiClient)


DriveApi是一个接口,因此没有任何方法实现。而且,getAppFolder()是实例方法,而不是静态方法。因此,您需要的是DriveApi的实现,可以从Drive获得:

DriveFolder appFolder = Drive.DriveApi.getAppFolder(googleApiClient)

07-24 09:47