我正在制作一个Android地图应用程序,使用goggle maps v2,alla一直运行直到我运行应用程序,它崩溃了。这是我的档案:
MainActivity.java
package com.geo.app;
import android.app.Activity;
import android.app.Fragment;
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.SupportMapFragment;
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 FragmentActivity {
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=((SupportMapFragment)getSupportFragmentManager().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);
}
}
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"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geo.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<permission
android:name="com.geo.app.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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.geo.app.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="AIzaSyB0MLq8L4YCA9GC05iD1V7OYfqlkUa0lhA" />
</application>
</manifest>
应用程序崩溃了,我在LogCat上有这个
04-10 10:59:02.545 11918-11918/? D/AndroidRuntime﹕ >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-10 10:59:02.545 11918-11918/? D/AndroidRuntime﹕ CheckJNI is OFF
04-10 10:59:02.725 11918-11918/? D/AndroidRuntime﹕ --- registering native functions ---
04-10 10:59:03.495 104-205/? I/ActivityManager﹕ Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.geo.app/.MainActivity }
04-10 10:59:03.605 11918-11918/? D/AndroidRuntime﹕ Shutting down VM
04-10 10:59:03.605 11918-11923/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
04-10 10:59:03.625 11918-11926/? I/AndroidRuntime﹕ NOTE: attach of thread 'Binder Thread #3' failed
04-10 10:59:03.675 5325-5325/? D/PackageAddedReceiver﹕ package added com.geo.app
04-10 10:59:03.695 104-107/? I/ActivityManager﹕ Start proc com.geo.app for activity com.geo.app/.MainActivity: pid=11928 uid=10082 gids={3003, 1015}
04-10 10:59:04.175 182-182/? D/dalvikvm﹕ GC_EXPLICIT freed 5596 objects / 283440 bytes in 5136ms
04-10 10:59:04.275 3332-11910/? D/RegisterService﹕ insert plugin size 1
04-10 10:59:04.335 104-243/? D/Sensors﹕ open_akm, fd=148
04-10 10:59:04.625 77-77/? D/AK8973﹕ Compass Start
04-10 10:59:04.745 11928-11928/com.geo.app W/dalvikvm﹕ VFY: unable to resolve static field 3704 (MapAttrs) in Lcom/google/android/gms/R$styleable;
04-10 10:59:04.745 11928-11928/com.geo.app D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x000e
04-10 10:59:04.745 11928-11928/com.geo.app D/dalvikvm﹕ VFY: dead code 0x0010-00ae in Lcom/google/android/gms/maps/GoogleMapOptions;.createFromAttributes (Landroid/content/Context;Landroid/util/AttributeSet;)Lcom/google/android/gms/maps/GoogleMapOptions;
04-10 10:59:04.755 11928-11928/com.geo.app D/AndroidRuntime﹕ Shutting down VM
04-10 10:59:04.755 11928-11928/com.geo.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40028a00)
04-10 10:59:04.775 11928-11928/com.geo.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1654)
at com.geo.app.MainActivity.onCreate(MainActivity.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
04-10 10:59:04.845 104-244/? W/ActivityManager﹕ Force finishing activity com.geo.app/.MainActivity
答案 0 :(得分:1)
在MainActivity.java类中更改片段导入语句
import android.support.v4.app.Fragment
答案 1 :(得分:1)
您在清单文件中缺少元标记
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
您不需要这些
<permission
android:name="com.geo.app.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
最好在初始化GoogleMap
对象之前检查Google Play服务的可用性
编辑:
java.lang.NoClassDefFoundError:com.google.android.gms.R $ styleable
您尚未在Android地图项目中引用Google Play服务库项目。