Google Map V2无效

时间:2014-01-25 18:27:18

标签: android

我尝试了很多但没有运气。我收到错误

01-25 23:32:07.115: E/AndroidRuntime(22407): FATAL EXCEPTION: main
01-25 23:32:07.115: E/AndroidRuntime(22407): java.lang.RuntimeException: Unable to start activity ComponentInfo{aakib.ahmed.mapapp/aakib.ahmed.mapapp.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread.access$600(ActivityThread.java:146)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1239)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.os.Looper.loop(Looper.java:137)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread.main(ActivityThread.java:5085)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at java.lang.reflect.Method.invoke(Method.java:511)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at dalvik.system.NativeStart.main(Native Method)
01-25 23:32:07.115: E/AndroidRuntime(22407): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Activity.setContentView(Activity.java:1881)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at aakib.ahmed.mapapp.MainActivity.onCreate(MainActivity.java:11)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Activity.performCreate(Activity.java:5104)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
01-25 23:32:07.115: E/AndroidRuntime(22407):    ... 11 more
01-25 23:32:07.115: E/AndroidRuntime(22407): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Fragment.instantiate(Fragment.java:585)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Fragment.instantiate(Fragment.java:560)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.app.Activity.onCreateView(Activity.java:4709)
01-25 23:32:07.115: E/AndroidRuntime(22407):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-25 23:32:07.115: E/AndroidRuntime(22407):    ... 21 more
01-25 23:32:09.057: I/Process(22407): Sending signal. PID: 22407 SIG: 9

我的activity_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.SupportMapFragment"/>

</RelativeLayout>

我的MainActivity.java

package aakib.ahmed.mapapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="aakib.ahmed.mapapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />
    <permission
        android:name="aakib.ahmed.mapapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="aakib.ahmed.mapapp.permission.MAPS_RECEIVE"/>
    <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"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application ..
    <activity ...
       </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="I removed it"/>
    </application>
</manifest>

我曾尝试更改Activity和MapFragment的FragmentActivity而不是SupportMapFragment但无法运行。我正在使用Android 4.2.2设备进行测试。该应用程序在启动后立即崩溃。

2 个答案:

答案 0 :(得分:0)

您在非支持活动中使用支持库片段。要么使MainActivity扩展FragmentActivity,要么在activity.xml中将SupportMapFragment更改为MapFragment

答案 1 :(得分:0)

你有

  <uses-sdk
    android:minSdkVersion="12

所以你应该使用MapFramgent。

更改

     <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

TP

    <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>

你也错过了

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

在清单文件的应用程序标记中。

确保您按照文档中的所有步骤进行操作

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2