我实施了Google Maps tutorial from this link。但我收到运行时错误,应用程序崩溃了。请帮助我。我跟着其他类似的问题,但一直无法找到解决方案。问题似乎是在我更新Google Play服务之后。
主要活动:
package com.example.sahayog;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat:
03-06 12:21:26.660: E/AndroidRuntime(12319): FATAL EXCEPTION: main
03-06 12:21:26.660: E/AndroidRuntime(12319): Process: com.example.sahayog, PID: 12319
03-06 12:21:26.660: E/AndroidRuntime(12319): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/maps/model/LatLng;
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.example.sahayog.MainActivity.<clinit>(MainActivity.java:16)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Constructor.newInstance(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.Class.newInstance(Class.java:1572)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.os.Looper.loop(Looper.java:135)
03-06 12:21:26.660: E/AndroidRuntime(12319): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Method.invoke(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.reflect.Method.invoke(Method.java:372)
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-06 12:21:26.660: E/AndroidRuntime(12319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-06 12:21:26.660: E/AndroidRuntime(12319): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.model.LatLng" on path: DexPathList[[zip file "/data/app/com.example.sahayog-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
03-06 12:21:26.660: E/AndroidRuntime(12319): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
03-06 12:21:26.660: E/AndroidRuntime(12319): ... 15 more
03-06 12:21:26.660: E/AndroidRuntime(12319): Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.maps.model.LatLng
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.Class.classForName(Native Method)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
03-06 12:21:26.660: E/AndroidRuntime(12319): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
03-06 12:21:26.660: E/AndroidRuntime(12319): ... 16 more
03-06 12:21:26.660: E/AndroidRuntime(12319): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
答案 0 :(得分:0)
试试这个:
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS)
{
// Google Play Services are not available
Toast.makeText(mContext, "Please install a maps application", Toast.LENGTH_LONG).show();
}
else
{
// Google Play Services are available
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}