0

我的谷歌地圖v2問題存在嚴重問題。 這裏是我的代碼在谷歌地圖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 

我已經嘗試過所有可能的解決方案,但我沒有一個幫助過我,我希望這裏的某個人能幫助我。

回答

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" 

使用支持fra gment manager and cast properly too:

map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)) 
     .getMap(); 
相關問題