我正在尝试使用谷歌地图API v2开发Android应用程序并尝试本教程,但我遇到了一些错误。我最后提到了。
教程紧随其后 Here
MainActivity.java
package com.example.bushunter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 HMBURG = 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(HMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("kiel")
.snippet("kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bushunter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.bushunter.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.bushunter.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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.maps.v2.API_KEY"
android:value="AIzaSyAL1CFSYV5jjCcG5Cj-BEQ5KprLBXZ5n4c" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
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="com.example.bushunter.MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
并发现以下错误.plz帮助
04-14 13:20:58.880: E/dalvikvm(16036): Could not find class 'com.google.android.gms.maps.model.LatLng', referenced from method com.example.bushunter.MainActivity.<clinit>
04-14 13:20:58.880: W/dalvikvm(16036): VFY: unable to resolve new-instance 448 (Lcom/google/android/gms/maps/model/LatLng;) in Lcom/example/bushunter/MainActivity;
04-14 13:20:58.880: D/dalvikvm(16036): VFY: replacing opcode 0x22 at 0x0000
04-14 13:20:58.890: E/dalvikvm(16036): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method com.example.bushunter.MainActivity.onCreate
04-14 13:20:58.890: W/dalvikvm(16036): VFY: unable to resolve check-cast 445 (Lcom/google/android/gms/maps/MapFragment;) in Lcom/example/bushunter/MainActivity;
04-14 13:20:58.890: D/dalvikvm(16036): VFY: replacing opcode 0x1f at 0x0014
04-14 13:20:58.890: D/dalvikvm(16036): DexOpt: unable to opt direct call 0x0db1 at 0x0c in Lcom/example/bushunter/MainActivity;.<clinit>
04-14 13:20:58.890: D/dalvikvm(16036): DexOpt: unable to opt direct call 0x0db1 at 0x1d in Lcom/example/bushunter/MainActivity;.<clinit>
04-14 13:20:58.890: D/dalvikvm(16036): DexOpt: unable to opt direct call 0x0db2 at 0x20 in Lcom/example/bushunter/MainActivity;.onCreate
04-14 13:20:58.890: D/dalvikvm(16036): DexOpt: unable to opt direct call 0x0db2 at 0x37 in Lcom/example/bushunter/MainActivity;.onCreate
04-14 13:20:58.890: W/dalvikvm(16036): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/example/bushunter/MainActivity;
04-14 13:20:58.890: W/dalvikvm(16036): Class init failed in newInstance call (Lcom/example/bushunter/MainActivity;)
04-14 13:20:58.890: D/AndroidRuntime(16036): Shutting down VM
04-14 13:20:58.890: W/dalvikvm(16036): threadid=1: thread exiting with uncaught exception (group=0x41838da0)
04-14 13:20:58.890: E/AndroidRuntime(16036): FATAL EXCEPTION: main
04-14 13:20:58.890: E/AndroidRuntime(16036): Process: com.example.bushunter, PID: 16036
04-14 13:20:58.890: E/AndroidRuntime(16036): java.lang.NoClassDefFoundError: com.google.android.gms.maps.model.LatLng
04-14 13:20:58.890: E/AndroidRuntime(16036): at com.example.bushunter.MainActivity.<clinit>(MainActivity.java:17)
04-14 13:20:58.890: E/AndroidRuntime(16036): at java.lang.Class.newInstanceImpl(Native Method)
04-14 13:20:58.890: E/AndroidRuntime(16036): at java.lang.Class.newInstance(Class.java:1208)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.ActivityThread.access$900(ActivityThread.java:172)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.os.Looper.loop(Looper.java:146)
04-14 13:20:58.890: E/AndroidRuntime(16036): at android.app.ActivityThread.main(ActivityThread.java:5653)
04-14 13:20:58.890: E/AndroidRuntime(16036): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:20:58.890: E/AndroidRuntime(16036): at java.lang.reflect.Method.invoke(Method.java:515)
04-14 13:20:58.890: E/AndroidRuntime(16036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
04-14 13:20:58.890: E/AndroidRuntime(16036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
04-14 13:20:58.890: E/AndroidRuntime(16036): at dalvik.system.NativeStart.main(Native Method)