【问题标题】:Google Maps v2 not loading. Error android.view.InflateException: Binary XML file line.. Error inflating class fragment谷歌地图 v2 未加载。 Error android.view.InflateException: Binary XML file line.. 膨胀类片段时出错
【发布时间】:2015-10-01 16:02:41
【问题描述】:

这是我的 MainActivity。我的应用程序中的最低 sdk 版本是 11。我在 android 清单文件中使用了 API 密钥。当我做了一些小的修改时,它显示的是空白而不是地图。

public class MainActivity extends Activity {

// Google Map
private GoogleMap googleMap;

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

    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}


}

这是我的布局 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

</RelativeLayout>

这是我的 Manifest.xml

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

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

<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" />

<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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="(MY API KEY)" />   
</application>

</manifest>

【问题讨论】:

  • 使用SupportMapFragment 而不是MapFragment
  • 一开始用FragmentActivity代替Activity

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


【解决方案1】:

提及这个元数据标签

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

删除 open gls tan 并让我知道结果

【讨论】:

  • 同样的例外,但现在是第 1 行。已更改为 7 而不是 6。
  • 从布局中移除 android:name="com.prasad.maptrial.MainActivity"
  • 为我工作。但这背后的问题是什么。那个元数据是怎么回事。而且我还在布局文件中包含了该行,因为它为stackoverflow中遇到相同问题的一些人提供了解决方案
【解决方案2】:

试试这个

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

【讨论】:

    【解决方案3】:

    你可以使用

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.your_id))
                .getMap();
    

    来自 xml

    <fragment
    android:id="@+id/your_id"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    

    在此处添加您的密钥

     android:value="(MY API KEY)"
    

    【讨论】:

    • 设置android:minSdkVersion="15"会更好
    • 感谢您花一些时间在这里。我的问题解决了。我标记了它。我也会尝试您的解决方案。顺便说一句,我在那里使用了我的钥匙。我想如果我透露它会是一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-07-26
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多