在Google地图v2中输入类片段时出错

时间:2014-06-05 11:39:56

标签: android google-maps google-maps-android-api-2 android-fragmentactivity mapfragment

我对谷歌地图v2的问题存在严重问题。 这是我的代码

MainActivity.java

package com.example.newsimplemap;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
GoogleMap map;

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

     map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)) .getMap();
    }


}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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" />

</LinearLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newsimplemap"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" /> 

    <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"     /> 
    <uses-permission 
    android:name="com.example.simplemap.permission.MAPS_RECEIVE" />

    <permission 
    android:name="com.example.simplemap.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.newsimplemap.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="API_KEY" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

</manifest>

这是错误日志

06-05 14:34:35.732: E/AndroidRuntime(3374): FATAL EXCEPTION: main

06-05 14:34:35.732: E/AndroidRuntime(3374): java.lang.RuntimeException: Unable to 
start activity 
ComponentInfo{com.example.newsimplemap/com.example.newsimplemap.MainActivity}: 
android.view.InflateException: Binary XML file line #10: Error inflating class fragment

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2110)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.ActivityThread.access$600(ActivityThread.java:138)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 

    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.os.Handler.dispatchMessage(Handler.java:99)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.os.Looper.loop(Looper.java:137)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.ActivityThread.main(ActivityThread.java:4940)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
java.lang.reflect.Method.invokeNative(Native Method)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
java.lang.reflect.Method.invoke(Method.java:511)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
dalvik.system.NativeStart.main(Native Method)

06-05 14:34:35.732: E/AndroidRuntime(3374): Caused by: android.view.InflateException: 
Binary XML file line #10: Error inflating class fragment

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.rInflate(LayoutInflater.java:746)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.inflate(LayoutInflater.java:489)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.inflate(LayoutInflater.java:396)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.inflate(LayoutInflater.java:352)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:425)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.Activity.setContentView(Activity.java:2122)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.example.newsimplemap.MainActivity.onCreate(MainActivity.java:15)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.Activity.performCreate(Activity.java:5275)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)

06-05 14:34:35.732: E/AndroidRuntime(3374):     ... 11 more

06-05 14:34:35.732: E/AndroidRuntime(3374): Caused by: java.lang.SecurityException: 
The Maps API requires the additional following permissions to be set in the 
AndroidManifest.xml to ensure a correct behavior:

06-05 14:34:35.732: E/AndroidRuntime(3374): <uses-permission 
android:name="android.permission.ACCESS_NETWORK_STATE"/>

06-05 14:34:35.732: E/AndroidRuntime(3374):     at lzv.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at mcg.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at mcg.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at mbi.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at lxn.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at lxm.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at fnb.onTransact(SourceFile:107)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.os.Binder.transact(Binder.java:326)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown 
Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.dynamic.a$4.b(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.dynamic.a.a(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.dynamic.a.onCreateView(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)

06-05 14:34:35.732: E/AndroidRuntime(3374):     at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

06-05 14:34:35.732: E/AndroidRuntime(3374):     ... 21 more

我已经尝试了所有可能的解决方案,但是没有一个能帮助我,我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

错误非常具体:

06-05 14:34:35.732: E/AndroidRuntime(3374): Caused by: java.lang.SecurityException: 
The Maps API requires the additional following permissions to be set in the 
AndroidManifest.xml to ensure a correct behavior:

06-05 14:34:35.732: E/AndroidRuntime(3374): <uses-permission 
android:name="android.permission.ACCESS_NETWORK_STATE"/>

然后将此添加到您的清单中:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

我注意到的另一件事是你正在使用SupportMapFragment:

class="com.google.android.gms.maps.SupportMapFragment"

使用支持片段管理器并正确投射:

map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
        .getMap();