我尝试在Android 4.2.2(API 17)上使用GoogleApiClient检索最近的位置,并出现以下异常:
12-26 17:31:34.591: I/dalvikvm(1015): Failed resolving Lcom/view/movieratingapp/MainActivity; interface 480 'Lcom/google/android/gms/common/api/GoogleApiClient$ConnectionCallbacks;'
12-26 17:31:34.591: W/dalvikvm(1015): Link of class 'Lcom/view/movieratingapp/MainActivity;' failed
12-26 17:31:42.142: W/jdwp(1015): Debugger is telling the VM to exit with code=1
12-26 17:31:42.142: I/dalvikvm(1015): GC lifetime allocation: 16367 bytes
12-26 17:31:42.451: E/Trace(1119): error opening trace file: No such file or directory (2)
12-26 17:31:42.512: I/dalvikvm(1119): Failed resolving Lcom/view/movieratingapp/MainActivity; interface 480 'Lcom/google/android/gms/common/api/GoogleApiClient$ConnectionCallbacks;'
12-26 17:31:42.512: W/dalvikvm(1119): Link of class 'Lcom/view/movieratingapp/MainActivity;' failed
12-26 17:31:42.521: D/AndroidRuntime(1119): Shutting down VM
12-26 17:31:42.521: W/dalvikvm(1119): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-26 17:31:42.541: E/AndroidRuntime(1119): FATAL EXCEPTION: main
12-26 17:31:42.541: E/AndroidRuntime(1119): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.view.movieratingapp/com.view.movieratingapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.view.movieratingapp.MainActivity" on path: /data/app/com.view.movieratingapp-2.apk
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.os.Looper.loop(Looper.java:137)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-26 17:31:42.541: E/AndroidRuntime(1119): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 17:31:42.541: E/AndroidRuntime(1119): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 17:31:42.541: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 17:31:42.541: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 17:31:42.541: E/AndroidRuntime(1119): at dalvik.system.NativeStart.main(Native Method)
12-26 17:31:42.541: E/AndroidRuntime(1119): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.view.movieratingapp.MainActivity" on path: /data/app/com.view.movieratingapp-2.apk
12-26 17:31:42.541: E/AndroidRuntime(1119): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-26 17:31:42.541: E/AndroidRuntime(1119): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-26 17:31:42.541: E/AndroidRuntime(1119): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-26 17:31:42.541: E/AndroidRuntime(1119): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-26 17:31:42.541: E/AndroidRuntime(1119): ... 11 more
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.view.movieratingapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
这是MainActivity文件:
package com.view.movieratingapp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.DialogFragment;
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.location.LocationServices;
public class MainActivity extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private double latitude = 0;
private double longitude = 0;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
/**
* Called when the Activity becomes visible.
*/
@Override
protected void onStart() {
super.onStart();
// Connect the client.
if (isGooglePlayServicesAvailable()) {
mGoogleApiClient.connect();
}
}
/**
* Called when the Activity is no longer visible.
*/
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
/**
* Handle results returned to the FragmentActivity by Google Play services
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST:
/*
* If the result code is Activity.RESULT_OK, try to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK:
mGoogleApiClient.connect();
break;
}
}
}
/**
* Check if GooglePlayServices is available before trying to use any
* Google APIs
* @return
*/
private boolean isGooglePlayServicesAvailable() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("Location Updates", "Google Play services is available.");
return true;
} else {
// Get the error dialog from Google Play services
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
errorFragment.setDialog(errorDialog);
errorFragment.show(getSupportFragmentManager(), "Location Updates");
}
return false;
}
}
/**
* Called by Location Services when the request to connect the client
* finishes successfully. At this point, you can request the current
* location or start periodic updates
*/
@Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
//Location location = mLocationClient.getLastLocation();
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
Toast.makeText(this, "GPS location was found!", Toast.LENGTH_SHORT).show();
latitude = mLastLocation.getLatitude();
longitude = mLastLocation.getLongitude();
} else {
Toast.makeText(this, "Current location was null, enable GPS on emulator!", Toast.LENGTH_SHORT).show();
}
}
/**
* Called by Location Services if the connection to the location client
* drops because of an error.
*/
public void onDisconnected() {
// Display the connection status
Toast.makeText(this, "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}
/**
* Called by Location Services if the attempt to get Location Services fails.
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/*
* Google Play services can resolve some errors it detects. If the error
* has a resolution, try sending an Intent to start a Google Play
* services activity that can resolve error.
*/
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(),
"Sorry. Location services not available to you", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int cause) {
// TODO Auto-generated method stub
}
/**
* Define a DialogFragment that displays the error dialog
*/
public static class ErrorDialogFragment extends DialogFragment {
// Global field to contain the error dialog
private Dialog mDialog;
// Default constructor. Sets the dialog field to null
public ErrorDialogFragment() {
super();
mDialog = null;
}
// Set the dialog to display
public void setDialog(Dialog dialog) {
mDialog = dialog;
}
// Return a Dialog to the DialogFragment.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialog;
}
}
}
在属性中 - &gt; android,GooglePlayServices rev 22在库部分设置,IsLibrary未勾选。
Project Build Target是Google API 17
正在使用的SDK构建工具是18.1.1
请提供有关为何会发生这种情况的建议
答案 0 :(得分:0)
当我尝试使用第三方库时遇到了同样的问题。
基本上,该库正在实施Google定位服务,因此我必须将这一行添加到Gradle文件中:
implementation 'com.google.android.gms:play-services-awareness:15.0.1'
希望这对以后有帮助:)