Google Maps Android API v2应用程序崩溃了

时间:2013-09-27 12:31:04

标签: android google-maps

我试图获取Android' Google Maps Android API v2 '的示例代码。工作。我没有错误地构建项目。但是,当我尝试在Everpad设备中运行该应用程序时,该应用程序立即崩溃。 这是:

- java代码

   package com.dvp.android.gallery;

    public class GPS 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.gps);
        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);
      }


}
  • The Manifest:

                          

        <application android:icon="@drawable/ic_bardo" android:label="@string/app_name">
            <activity android:name=".Acceuil"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".Objets" android:label="Objets">  </activity>
            <activity android:name=".GPS"
                      android:label="GPS">
            </activity>
            <activity android:name=".Detail" android:label="Detail"></activity>
            <activity
                    android:name=".Galeries"
                    android:label="Galeries"
                    />
            <activity
                    android:name=".Evennements"
                    android:label="Evennements"/>
            <activity
                    android:name=".Infos"
                    android:label="Infos"/>
        </application>
        <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyA0HMTTGLqlYaq6jOuS0imbjt7GmUHyK0c"/>
    
        <uses-sdk android:minSdkVersion="11" />
    
    
    
    
            <uses-permission android:name="com.dvp.android.gallery.maps.permission.MAPS_RECEIVE" />
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
            <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" />
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
        </manifest> 
    
    • logcat输出:

09-27 12:42:49.466:E / AndroidRuntime(5542):java.lang.RuntimeException:无法启动活动ComponentInfo {com.dvp.android.gallery/com.dvp.android.gallery.GPS}: android.view.InflateException:二进制XML文件行#11:错误膨胀类片段 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.os.Handler.dispatchMessage(Handler.java:99) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.os.Looper.loop(Looper.java:137) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread.main(ActivityThread.java:4429) 09-27 12:42:49.466:E / AndroidRuntime(5542):at java.lang.reflect.Method.invokeNative(Native Method) 09-27 12:42:49.466:E / AndroidRuntime(5542):at java.lang.reflect.Method.invoke(Method.java:511) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:795) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 09-27 12:42:49.466:E / AndroidRuntime(5542):at dalvik.system.NativeStart.main(Native Method) 09-27 12:42:49.466:E / AndroidRuntime(5542):引起:android.view.InflateException:二进制XML文件行#11:错误膨胀类片段 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.view.LayoutInflater.inflate(LayoutInflater.java:489) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.view.LayoutInflater.inflate(LayoutInflater.java:352) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.Activity.setContentView(Activity.java:1835) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.dvp.android.gallery.GPS.onCreate(GPS.java:23) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.Activity.performCreate(Activity.java:4465) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 09-27 12:42:49.466:E / AndroidRuntime(5542):... 11更多 09-27 12:42:49.466:E / AndroidRuntime(5542):引起:java.lang.RuntimeException:找不到API密钥。检查它是否在AndroidManifest.xml的元素中 09-27 12:42:49.466:E / AndroidRuntime(5542):at maps.ag.bb.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at maps.ag.bb.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at maps.ag.an.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at maps.ag.bh.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at maps.ag.bg.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at bob.onTransact(SourceFile:107) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.os.Binder.transact(Binder.java:297) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.maps.internal.IMapFragmentDelegate $ a $ a.onCreateView(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.maps.MapFragment $ a.onCreateView(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.dynamic.a $ 4.b(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.dynamic.a.a(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.dynamic.a.onCreateView(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:806) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010) 09-27 12:42:49.466:E / AndroidRuntime(5542):在android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1108)

XML:

  <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".GPS" >
      <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" />

    </LinearLayout>

请帮帮我。谢谢

2 个答案:

答案 0 :(得分:0)

使用FragmentActivity扩展您的活动,并将您的布局map-id称为SupportMapFragment。

/ ** * @author manish * * /

public class DrawMap extends FragmentActivity implements OnMapClickListener,
        OnMapLongClickListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_map);

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        googleMap.setOnMapClickListener(this);
        googleMap.setOnMapLongClickListener(this);
    }

你的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" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" class="com.google.android.gms.maps.SupportMapFragment" /> </RelativeLayout>

了解详情:http://www.androidhub4you.com/2013/07/google-map-version-2-integration-in_8530.html#ixzz2g67icLyr

答案 1 :(得分:0)

您必须使Activity扩展 FragmentActivity 而不是活动

这是因为Google Map类有一个片段,为此您必须导入 android.support.v4。 jar文件...并将地图添加到您需要的片段中作为片段通过 FragmentActivity

进行扩展