【问题标题】:error while inflating XML in Google maps fragment在 Google 地图片段中膨胀 XML 时出错
【发布时间】:2012-12-16 20:36:43
【问题描述】:

尝试使用片段显示 Google 地图。使用以下page 作为教程。

我收到异常“错误膨胀类片段”。

1) 导入jar google-play-services.jar

2) 下载并配置了google play services SDK。

3) 获得最新的 v2 API 密钥。

4) 在清单中添加了权限 com.google.android.providers.gsf.permission.READ_GSERVICES。

5) 使用 mindsdk = 8 和 target = 16。

供参考,Androidmanifest.xml

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

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

<permission
android:name="com.example.com.mapsdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.com.mapsdemo.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.com.mapsdemo.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="AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk"/>


</application>

MainActivity.java

package com.example.com.mapsdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesUtil;


//API : AIzaSyDXPsxWF634gd907NzZKkRkNS0oH9IPWgk

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    try {
    setContentView(R.layout.activity_main);
    } catch (Exception e) {

    Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/map"
 android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

Logcat:

01-02 18:31:21.477: I/jdwp(11473): Ignoring second debugger -- accepting and dropping
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-02 18:31:24.493: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-02 18:31:24.509: D/libEGL(11871): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-02 18:31:24.602: D/OpenGLRenderer(11871): Enabling debug mode 0

【问题讨论】:

  • 请发布堆栈跟踪。
  • 我是新手。如果我做错了请告诉我
  • 这不是与您的问题相关的 Java 堆栈跟踪。在调试器之外运行应用程序,允许发生崩溃,并查看 LogCat 的堆栈跟踪。
  • 我可以在 logcat 上看到这个。车祸发生在那个时候。你的意思是我手动安装apk并运行?还是这样做会有好处?
  • 这不是堆栈跟踪,句号。

标签: android xml google-maps android-inflate inflate-exception


【解决方案1】:

Error inflating class fragment 通常在您尝试使用原生 API 级别 11 版本的片段时出现——您的源代码正在这样做——但尝试在旧版本的 Android 上运行它。为了在 API 级别 10 及以下使用&lt;fragment&gt;,您必须使用 Android 支持包的片段的反向移植:

  • 将 Android 支持包添加到您的项目中
  • FragmentActivity 继承而不是Activity
  • 使用SupportMapFragment 而不是MapFragment
  • 根据此向后移植更改现在不同的任何其他内容

或者,您可以将 android:minSdkVersion 设置为 11 或更高,然后仅在较新的设备上运行您当前的代码。

您可以在the documentation 中阅读有关 Android 支持包的更多信息。

【讨论】:

  • 对于第二个修复,我将 Nexus 与果冻豆一起使用,但没有奏效。首先,即使在对四个点进行更改之后,它也会引发相同的异常。
  • 继承自 FragmentActivity 而不是 Activity
【解决方案2】:

我遇到了同样的错误。检查您是否在真实设备上运行它。并以下列方式附加谷歌播放服务库。 project_properties->android-> 库然后在那里添加 google play lib..

再次将 libs 文件夹复制 android support v4.jar 和 google_play_services.jar 然后右键单击每个文件并选择 build to path..

还要检查您的 api 密钥是否适用于 com.example.com.mapsdemo 包..对于每个包都将生成新的 api 密钥..

并在 XML 文件中的 MapFragment 上使用 SupportMapFragment。

这解决了我的问题。

希望这能解决您的问题。

【讨论】:

    【解决方案3】:

    很多关于这个问题的问题,两天后,我的解决方案很简单:你必须使用 Project Build Target Google API 编译你的项目。 我觉得这很愚蠢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多