谷歌地图v2错误膨胀类片段

时间:2014-03-10 11:23:07

标签: android google-maps-api-2 fatal-error inflate-exception

我在我的应用程序中使用谷歌地图v2。应用程序一启动就会崩溃。 Min SDK版本为8,Max为19.我无法得到错误。以下是我的LogCat和代码,请帮助

日志

03-10 16:17:37.977: E/AndroidRuntime(24296): FATAL EXCEPTION: main
03-10 16:17:37.977: E/AndroidRuntime(24296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appscourt.earth.map.location/com.appscourt.earth.map.location.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.os.Looper.loop(Looper.java:137)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread.main(ActivityThread.java:5059)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at java.lang.reflect.Method.invoke(Method.java:511)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at dalvik.system.NativeStart.main(Native Method)
03-10 16:17:37.977: E/AndroidRuntime(24296): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.Activity.setContentView(Activity.java:1893)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.appscourt.earth.map.location.MainActivity.onCreate(MainActivity.java:31)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.Activity.performCreate(Activity.java:5058)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-10 16:17:37.977: E/AndroidRuntime(24296):    ... 11 more
03-10 16:17:37.977: E/AndroidRuntime(24296): 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:
03-10 16:17:37.977: E/AndroidRuntime(24296): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.ch.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.ay.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.ay.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.al.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.bg.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at maps.e.bf.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at eio.onTransact(SourceFile:107)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.os.Binder.transact(Binder.java:326)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.dynamic.a$4.b(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.dynamic.a.a(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
03-10 16:17:37.977: E/AndroidRuntime(24296):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

XML文件

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

MainActivity.Java

public class MainActivity extends FragmentActivity implements
        LoaderCallbacks<Cursor> {
....
    private void initializeMap() {

        if (gMap == null) {

        gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();
        // check if map is created successfully or not
        if (gMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

要为Google地图充气,您需要在清单中添加以下权限。它在您的logcat错误中明确定义。

引起:java.lang.SecurityException:Maps API要求在AndroidManifest.xml中设置其他以下权限以确保正确行为:

 <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" />
    <!-- Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

答案 1 :(得分:1)

你的logcat清楚地说:

 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: 03-10 16:17:37.977: E/AndroidRuntime(24296): 
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

您需要在permission

中添加此manifest.xml
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 2 :(得分:1)

出于Internet安全原因,您需要将此权限添加到清单文件中。

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