我从 Android 开发开始,并从这里https://developers.google.com/maps/documentation/android/intro#sample_code使用示例代码访问 Google 地图。我按照本网站中的所有步骤进行操作,我这边没有任何改变,当我运行时,我在模拟器上看到这条消息:不幸的是 Google Maps API 已停止。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo"
android:versionCode="2"
android:versionName="2.1.0">
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission >android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<!-- End of copy. -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/demo_title"
android:hardwareAccelerated="true">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDYj6PVzqEyVw5ZItRp-PKrVEIb8woTdVc"/>
<activity android:name="com.example.mapdemo.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".BasicMapActivity"
android:label="@string/basic_map"/>
<activity
android:name=".CameraDemoActivity"
android:label="@string/camera_demo"/>
<activity
android:name=".CircleDemoActivity"
android:label="@string/circle_demo"/>
<activity
android:name=".EventsDemoActivity"
android:label="@string/events_demo"/>
<activity
android:name=".GroundOverlayDemoActivity"
android:label="@string/groundoverlay_demo"/>
<activity
android:name=".LayersDemoActivity"
android:label="@string/layers_demo"/>
<activity
android:name=".LocationSourceDemoActivity"
android:label="@string/locationsource_demo"/>
<activity
android:name=".MarkerDemoActivity"
android:label="@string/marker_demo"/>
<activity
android:name=".OptionsDemoActivity"
android:label="@string/options_demo"/>
<activity
android:name=".PolygonDemoActivity"
android:label="@string/polygon_demo"/>
<activity
android:name=".PolylineDemoActivity"
android:label="@string/polyline_demo"/>
<activity
android:name=".ProgrammaticDemoActivity"
android:label="@string/programmatic_demo"/>
<activity
android:name=".TileOverlayDemoActivity"
android:label="@string/tile_overlay_demo"/>
<activity
android:name=".UiSettingsDemoActivity"
android:label="@string/uisettings_demo"/>
<activity
android:name=".RawMapViewDemoActivity"
android:label="@string/raw_mapview_demo"/>
<activity
android:name=".RetainMapActivity"
android:label="@string/retain_map"/>
<activity
android:name=".MultiMapDemoActivity"
android:label="@string/multi_map_demo"/>
</application>
</manifest>
这是main.java
文件:
package com.example.mapdemo;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.example.mapdemo.view.FeatureView;
import android.app.Activity;
public final class MainActivity extends ListActivity {
private static class DemoDetails {
private final int titleId;
private final int descriptionId;
private final Class<? extends FragmentActivity> activityClass;
public DemoDetails(
int titleId, int descriptionId, Class<? extends FragmentActivity> >activityClass) {
super();
this.titleId = titleId;
this.descriptionId = descriptionId;
this.activityClass = activityClass;
}
}
private static class CustomArrayAdapter extends ArrayAdapter<DemoDetails> {
public CustomArrayAdapter(Context context, DemoDetails[] demos) {
super(context, R.layout.feature, R.id.title, demos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FeatureView featureView;
if (convertView instanceof FeatureView) {
featureView = (FeatureView) convertView;
} else {
featureView = new FeatureView(getContext());
}
DemoDetails demo = getItem(position);
featureView.setTitleId(demo.titleId);
featureView.setDescriptionId(demo.descriptionId);
return featureView;
}
}
private static final DemoDetails[] demos = {
new DemoDetails(
R.string.basic_map, R.string.basic_description, BasicMapActivity.class),
new DemoDetails(R.string.camera_demo, R.string.camera_description,
CameraDemoActivity.class),
new DemoDetails(R.string.events_demo, R.string.events_description,
EventsDemoActivity.class),
new DemoDetails(R.string.layers_demo, R.string.layers_description,
LayersDemoActivity.class),
new DemoDetails(
R.string.locationsource_demo, R.string.locationsource_description,
LocationSourceDemoActivity.class),
new DemoDetails(R.string.uisettings_demo, R.string.uisettings_description,
UiSettingsDemoActivity.class),
new DemoDetails(R.string.groundoverlay_demo, >R.string.groundoverlay_description,
GroundOverlayDemoActivity.class),
new DemoDetails(R.string.marker_demo, R.string.marker_description,
MarkerDemoActivity.class),
new DemoDetails(R.string.polygon_demo, R.string.polygon_description,
PolygonDemoActivity.class),
new DemoDetails(R.string.polyline_demo, R.string.polyline_description,
PolylineDemoActivity.class),
new DemoDetails(R.string.circle_demo, R.string.circle_description,
CircleDemoActivity.class),
new DemoDetails(R.string.tile_overlay_demo, R.string.tile_overlay_description,
TileOverlayDemoActivity.class),
new DemoDetails(R.string.options_demo, R.string.options_description,
OptionsDemoActivity.class),
new DemoDetails(R.string.multi_map_demo, R.string.multi_map_description,
MultiMapDemoActivity.class),
new DemoDetails(R.string.retain_map, R.string.retain_map_description,
RetainMapActivity.class),
new DemoDetails(R.string.raw_mapview_demo, R.string.raw_mapview_description,
RawMapViewDemoActivity.class),
new DemoDetails(R.string.programmatic_demo, R.string.programmatic_description,
ProgrammaticDemoActivity.class)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListAdapter adapter = new CustomArrayAdapter(this, demos);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
DemoDetails demo = (DemoDetails) getListAdapter().getItem(position);
startActivity(new Intent(this, demo.activityClass));
}
}
这是 main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_demos"/>
</LinearLayout>
这是完整的logcat:
03-09 03:09:11.425: W/dalvikvm(2796): Unable to resolve superclass of Lcom/example/mapdemo/BasicMapActivity; (75)
03-09 03:09:11.425: W/dalvikvm(2796): Link of class 'Lcom/example/mapdemo/BasicMapActivity;' failed
03-09 03:09:11.455: E/dalvikvm(2796): Could not find class 'com.example.mapdemo.BasicMapActivity', referenced from method com.example.mapdemo.MainActivity.<clinit>
03-09 03:09:11.455: W/dalvikvm(2796): VFY: unable to resolve const-class 117 (Lcom/example/mapdemo/BasicMapActivity;) in Lcom/example/mapdemo/MainActivity;
03-09 03:09:11.455: D/dalvikvm(2796): VFY: replacing opcode 0x1c at 0x000d
03-09 03:09:11.485: W/dalvikvm(2796): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/example/mapdemo/MainActivity;
03-09 03:09:11.555: W/dalvikvm(2796): Class init failed in newInstance call (Lcom/example/mapdemo/MainActivity;)
03-09 03:09:11.555: D/AndroidRuntime(2796): Shutting down VM
03-09 03:09:11.555: W/dalvikvm(2796): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-09 03:09:11.585: E/AndroidRuntime(2796): FATAL EXCEPTION: main
03-09 03:09:11.585: E/AndroidRuntime(2796): java.lang.ExceptionInInitializerError
03-09 03:09:11.585: E/AndroidRuntime(2796): at java.lang.Class.newInstanceImpl(Native Method)
03-09 03:09:11.585: E/AndroidRuntime(2796): at java.lang.Class.newInstance(Class.java:1319)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.os.Looper.loop(Looper.java:137)
03-09 03:09:11.585: E/AndroidRuntime(2796): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-09 03:09:11.585: E/AndroidRuntime(2796): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 03:09:11.585: E/AndroidRuntime(2796): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 03:09:11.585: E/AndroidRuntime(2796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-09 03:09:11.585: E/AndroidRuntime(2796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-09 03:09:11.585: E/AndroidRuntime(2796): at dalvik.system.NativeStart.main(Native Method)
03-09 03:09:11.585: E/AndroidRuntime(2796): Caused by: java.lang.NoClassDefFoundError: com.example.mapdemo.BasicMapActivity
03-09 03:09:11.585: E/AndroidRuntime(2796): at com.example.mapdemo.MainActivity.<clinit>(MainActivity.java:98)
03-09 03:09:11.585: E/AndroidRuntime(2796): ... 15 more
03-09 03:09:15.014: I/Process(2796): Sending signal. PID: 2796 SIG: 9
我正在使用带有 API 17 Platform 4.2.2 Target Google API 和设备 5.1 "WVGA (480 * 800: mdpi) 的模拟器。
我怎么解决这个问题?
此外,我正在考虑购买设备三星 Galaxy Mini 2 S6500 以在其上运行我的应用程序而不是模拟器。该设备是否合适?
它有 512 MB RAM 和 Android 操作系统,v2.3.6 ( Gingerbread ),计划升级到 v4.1。