我正在尝试根据教程在我的 android 应用程序中查看地图,但应用程序每次都会意外关闭。我不知道如何解决它请帮助我。
我的MainActivity.java
代码:
package com.exple.sample_map;
import android.os.Bundle;
import android.app.Activity;
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;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
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);
try{
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);
}catch(Exception er)
{
Toast.makeText(MainActivity.this, er.getMessage(),Toast.LENGTH_LONG);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
try{// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
}catch(Exception er)
{
Toast.makeText(MainActivity.this, er.getMessage(),Toast.LENGTH_LONG);
}
return true;}
}
我的activity_main.xml
代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
我的清单文件代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exple.sample_map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.exple.sample_map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.exple.sample_map.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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="com.exple.sample_map.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.maps.v2.API_KEY"
android:value="AIzaSyCQ9wr3HnFnwlqzi4HyVUhDIfWtGSm-98c" />
</application>
</manifest>
删除 try catch 块后,logcat 跟踪是
05-22 21:12:05.008: E/dalvikvm(2188): Could not find class 'com.google.android.gms.maps.model.LatLng', referenced from method com.exple.sample_map.MainActivity.<clinit>
05-22 21:12:05.057: W/dalvikvm(2188): VFY: unable to resolve new-instance 483 (Lcom/google/android/gms/maps/model/LatLng;) in Lcom/exple/sample_map/MainActivity;
05-22 21:12:05.057: D/dalvikvm(2188): VFY: replacing opcode 0x22 at 0x0000
05-22 21:12:05.057: D/dalvikvm(2188): VFY: dead code 0x0002-0022 in Lcom/exple/sample_map/MainActivity;.<clinit> ()V
05-22 21:12:05.067: I/dalvikvm(2188): Could not find method com.exple.sample_map.MainActivity.getFragmentManager, referenced from method com.exple.sample_map.MainActivity.onCreate
05-22 21:12:05.067: W/dalvikvm(2188): VFY: unable to resolve virtual method 3388: Lcom/exple/sample_map/MainActivity;.getFragmentManager ()Landroid/app/FragmentManager;
05-22 21:12:05.067: D/dalvikvm(2188): VFY: replacing opcode 0x6e at 0x0008
05-22 21:12:05.067: D/dalvikvm(2188): VFY: dead code 0x000b-0073 in Lcom/exple/sample_map/MainActivity;.onCreate (Landroid/os/Bundle;)V
05-22 21:12:05.098: W/dalvikvm(2188): Exception Ljava/lang/NoClassDefFoundError; thrown during Lcom/exple/sample_map/MainActivity;.<clinit>
05-22 21:12:05.098: W/dalvikvm(2188): Class init failed in newInstance call (Lcom/exple/sample_map/MainActivity;)
05-22 21:12:05.117: D/AndroidRuntime(2188): Shutting down VM
05-22 21:12:05.117: W/dalvikvm(2188): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-22 21:12:05.137: E/AndroidRuntime(2188): FATAL EXCEPTION: main
05-22 21:12:05.137: E/AndroidRuntime(2188): java.lang.ExceptionInInitializerError
05-22 21:12:05.137: E/AndroidRuntime(2188): at java.lang.Class.newInstanceImpl(Native Method)
05-22 21:12:05.137: E/AndroidRuntime(2188): at java.lang.Class.newInstance(Class.java:1429)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.os.Looper.loop(Looper.java:123)
05-22 21:12:05.137: E/AndroidRuntime(2188): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 21:12:05.137: E/AndroidRuntime(2188): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 21:12:05.137: E/AndroidRuntime(2188): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 21:12:05.137: E/AndroidRuntime(2188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-22 21:12:05.137: E/AndroidRuntime(2188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-22 21:12:05.137: E/AndroidRuntime(2188): at dalvik.system.NativeStart.main(Native Method)
05-22 21:12:05.137: E/AndroidRuntime(2188): Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.maps.model.LatLng
05-22 21:12:05.137: E/AndroidRuntime(2188): at com.exple.sample_map.MainActivity.<clinit>(MainActivity.java:24)
05-22 21:12:05.137: E/AndroidRuntime(2188): ... 15 more
05-22 21:12:13.828: I/Process(2188): Sending signal. PID: 2188 SIG: 9