我正在尝试使用GooglePlaceAPI来获取当前位置 当我使用Google Play服务作为依赖项时,会导致无法找到我的主要活动的错误。 以下是代码:
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i("TAG", String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
likelyPlaces.release();
}
});
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
}
我的日志在这里:
09-12 21:53:51.796: W/ResourceType(9470): Found multiple library tables, ignoring...
09-12 21:53:51.802: I/art(9470): Rejecting re-init on previously-failed class java.lang.Class<com.example.myproject.MainActivity>
09-12 21:53:51.804: D/AndroidRuntime(9470): Shutting down VM
09-12 21:53:51.823: E/AndroidRuntime(9470): FATAL EXCEPTION: main
09-12 21:53:51.823: E/AndroidRuntime(9470): Process: com.example.myproject, PID: 9470
09-12 21:53:51.823: E/AndroidRuntime(9470): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myproject/com.example.myproject.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.myproject.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myproject-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread.access$800(ActivityThread.java:149)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.os.Looper.loop(Looper.java:211)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread.main(ActivityThread.java:5317)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.reflect.Method.invoke(Native Method)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.reflect.Method.invoke(Method.java:372)
09-12 21:53:51.823: E/AndroidRuntime(9470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
09-12 21:53:51.823: E/AndroidRuntime(9470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
09-12 21:53:51.823: E/AndroidRuntime(9470): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myproject.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myproject-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
09-12 21:53:51.823: E/AndroidRuntime(9470): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
09-12 21:53:51.823: E/AndroidRuntime(9470): ... 10 more
09-12 21:53:51.823: E/AndroidRuntime(9470): Suppressed: java.lang.NoClassDefFoundError: com.example.myproject.MainActivity
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.DexFile.defineClassNative(Native Method)
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.DexFile.defineClass(DexFile.java:226)
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
09-12 21:53:51.823: E/AndroidRuntime(9470): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
09-12 21:53:51.823: E/AndroidRuntime(9470): ... 14 more
09-12 21:53:51.823: E/AndroidRuntime(9470): Suppressed: java.lang.ClassNotFoundException: com.example.myproject.MainActivity
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.Class.classForName(Native Method)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
09-12 21:53:51.823: E/AndroidRuntime(9470): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
09-12 21:53:51.823: E/AndroidRuntime(9470): ... 13 more
09-12 21:53:51.823: E/AndroidRuntime(9470): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
09-12 21:53:51.848: I/Process(9470): Sending signal. PID: 9470 SIG: 9
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myproject.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.geo.API_KEY"
android:value="MY_KEY"/>
</application>
</manifest>
答案 0 :(得分:0)
在Manifest.xml文件中尝试此操作:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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.geo.API_KEY"
android:value="MY_KEY"/>
</application>
</manifest>
因为在Manifest的开头声明你的包名就足够了。