无法实例化活动com.google.android.gms.drive.sample.quickstart



我在stackoverflow中尝试了很多解决方案,但都不适合我。

  • 刷新+清洁项目安卓私人图书馆已检查(项目->属性->Java构建路径->订单&导出
  • 复制项目库(文件>导入,选择安卓>现有安卓代码进入工作区android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/>勾选库项目的副本

我只是遵循谷歌的官方教程
[谷歌快速启动源代码:https://developers.google.com/drive/android/examples/]

我还将我的Google Play Services SDK项目库包含到我当前的项目中。[https://developers.google.com/drive/android/get-started]

但是当我在我的android手机(android 4.1)上运行它时,我会收到这些错误
有人能帮我吗?我花了两天时间。

Logcat

09-03 15:21:39.050: W/dalvikvm(1615): Link of class 'Lcom/google/android/gms/drive/sample/quickstart/MainActivity;' failed
09-03 15:21:39.051: D/AndroidRuntime(1615): Shutting down VM
09-03 15:21:39.051: W/dalvikvm(1615): threadid=1: thread exiting with uncaught exception (group=0x41899908)
09-03 15:21:39.053: E/AndroidRuntime(1615): FATAL EXCEPTION: main
09-03 15:21:39.053: E/AndroidRuntime(1615): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.google.android.gms.drive.sample.quickstart/com.google.android.gms.drive.sample.quickstart.MainActivity}: java.lang.ClassNotFoundException: com.google.android.gms.drive.sample.quickstart.MainActivity
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2127)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.os.Looper.loop(Looper.java:153)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread.main(ActivityThread.java:5006)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invoke(Method.java:511)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at dalvik.system.NativeStart.main(Native Method)
09-03 15:21:39.053: E/AndroidRuntime(1615): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.drive.sample.quickstart.MainActivity
09-03 15:21:39.053: E/AndroidRuntime(1615):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-03 15:21:39.053: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2118)
09-03 15:21:39.053: E/AndroidRuntime(1615):     ... 11 more

清单

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright 2013 Google, Inc
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.drive.sample.quickstart"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.google.android.gms.drive.sample.quickstart.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>
</manifest>

主活动.java

/**
 * Copyright 2013 Google Inc. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.gms.drive.sample.quickstart;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.ContentsResult;
import com.google.android.gms.drive.MetadataChangeSet;
/**
 * Android Drive Quickstart activity. This activity takes a photo and saves it
 * in Google Drive. The user is prompted with a pre-made dialog which allows
 * them to choose the file location.
 */
public class MainActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener {
    private static final String TAG = "android-drive-quickstart";
    private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
    private static final int REQUEST_CODE_CREATOR = 2;
    private static final int REQUEST_CODE_RESOLUTION = 3;
    private GoogleApiClient mGoogleApiClient;
    private Bitmap mBitmapToSave;
    /**
     * Create a new file and save it to Drive.
     */
    private void saveFileToDrive() {
        // Start by creating a new contents, and setting a callback.
        Log.i(TAG, "Creating new contents.");
        final Bitmap image = mBitmapToSave;
        Drive.DriveApi.newContents(mGoogleApiClient).setResultCallback(new ResultCallback<ContentsResult>() {
            @Override
            public void onResult(ContentsResult result) {
                // If the operation was not successful, we cannot do anything
                // and must
                // fail.
                if (!result.getStatus().isSuccess()) {
                    Log.i(TAG, "Failed to create new contents.");
                    return;
                }
                // Otherwise, we can write our data to the new contents.
                Log.i(TAG, "New contents created.");
                // Get an output stream for the contents.
                OutputStream outputStream = result.getContents().getOutputStream();
                // Write the bitmap data from it.
                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
                try {
                    outputStream.write(bitmapStream.toByteArray());
                } catch (IOException e1) {
                    Log.i(TAG, "Unable to write file contents.");
                }
                // Create the initial metadata - MIME type and title.
                // Note that the user will be able to change the title later.
                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                        .setMimeType("image/jpeg").setTitle("Android Photo.png").build();
                // Create an intent for the file chooser, and start it.
                IntentSender intentSender = Drive.DriveApi
                        .newCreateFileActivityBuilder()
                        .setInitialMetadata(metadataChangeSet)
                        .setInitialContents(result.getContents())
                        .build(mGoogleApiClient);
                try {
                    startIntentSenderForResult(
                            intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
                } catch (SendIntentException e) {
                    Log.i(TAG, "Failed to launch file chooser.");
                }
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            // Create the API client and bind it to an instance variable.
            // We use this instance as the callback for connection and connection
            // failures.
            // Since no account name is passed, the user is prompted to choose.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        // Connect the client. Once connected, the camera is launched.
        mGoogleApiClient.connect();
    }
    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }
    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_CAPTURE_IMAGE:
                // Called after a photo has been taken.
                if (resultCode == Activity.RESULT_OK) {
                    // Store the image data as a bitmap for writing later.
                    mBitmapToSave = (Bitmap) data.getExtras().get("data");
                }
                break;
            case REQUEST_CODE_CREATOR:
                // Called after a file is saved to Drive.
                if (resultCode == RESULT_OK) {
                    Log.i(TAG, "Image successfully saved.");
                    mBitmapToSave = null;
                    // Just start the camera again for another photo.
                    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                            REQUEST_CODE_CAPTURE_IMAGE);
                }
                break;
        }
    }
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Called whenever the API client fails to connect.
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }
        // The failure has a resolution. Resolve it.
        // Called typically when the app is not yet authorized, and an
        // authorization
        // dialog is displayed to the user.
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");
        if (mBitmapToSave == null) {
            // This activity has no UI of its own. Just start the camera.
            startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                    REQUEST_CODE_CAPTURE_IMAGE);
            return;
        }
        saveFileToDrive();
    }
    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }
}

我通过将SDK构建工具更新到最新版本(20.0版)解决了这个问题。

Window > Android SDK Manager > Tools > select the latest Android SDK Build-tools

我注意到我的问题是Google Play服务版本是19,但我的SDK构建工具是18.0.1
在我将SDK构建工具更新到最新版本后,问题就解决了。

最新更新