【问题标题】:Setting up Google maps API v2 on android, Error inflating class fragment在 android 上设置 Google maps API v2,错误膨胀类片段
【发布时间】:2013-03-07 02:18:45
【问题描述】:

所以我正在关注 Android 版 Google 地图 API v2,试图在屏幕上显示基本地图。完成所有步骤后,我得到一个“错误膨胀类片段”其他人遇到了同样的问题,但似乎没有一个解决方案有效。 确切错误:android.view.InflateException: Binary XML file line #10: Error inflating class fragment

.java:

import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;


public class GoogleMaps extends FragmentActivity {

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

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

}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GoogleMaps" >
    <fragment 
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:name="com.testing.svma.MainActivity"/>
</RelativeLayout>

我认为我已经正确设置了 Google Play 服务,它会在第一次导航到页面时要求下载它们。这是我的 project.properties:

# Project target.
target=android-17
android.library=false
android.library.reference.1=../../../android-sdks/extras/google/google_play_services/libproject/google-play-services_lib

日志猫:

...
03-06 21:12:44.751: I/System.out(9122): Debugger has connected
03-06 21:12:44.751: I/System.out(9122): waiting for debugger to settle...
03-06 21:12:46.166: I/System.out(9122): debugger has settled (1487)
03-06 21:12:50.634: D/dalvikvm(9122): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
03-06 21:12:50.634: W/dalvikvm(9122): VFY: unable to resolve instance field 23
03-06 21:12:50.634: D/dalvikvm(9122): VFY: replacing opcode 0x52 at 0x0012
03-06 21:12:50.634: D/dalvikvm(9122): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/p/s; (427)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/p/s;' failed
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/y/bo; (3820)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/y/bo;' failed
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/i/k; (4208)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/i/k;' failed
03-06 21:12:51.126: E/dalvikvm(9122): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
03-06 21:12:51.126: W/dalvikvm(9122): VFY: unable to resolve new-instance 3540 (Lmaps/i/k;) in Lmaps/z/ag;
03-06 21:12:51.126: D/dalvikvm(9122): VFY: replacing opcode 0x22 at 0x006d
03-06 21:12:51.149: D/dalvikvm(9122): VFY: dead code 0x006f-007f in Lmaps/z/ag;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;ZLjava/lang/String;)Lmaps/z/ag;

我已经尝试了这些线程的解决方案,但没有运气:

Error inflating class fragment

Google Maps V2 - Error inflating class Fragment

我也只是按照这个教程,得到了同样的错误:

https://www.youtube.com/watch?v=OZRgiwGfvSQ

【问题讨论】:

  • 这里也一样!你得到这个工作了吗?
  • 是的,不知道是怎么回事,做了一些事情...您是否将 Google Play 服务库复制到项目目录中,而不是在 sdk 目录中引用它?
  • 我有这个示例使用带有 Activity 的 googlemaps v2 使用 FragmentActivity 用于较低的操作系统和更高的操作系统以及最新的 Android 支持库。这很困难,但有可能。看这个例子:android-er.blogspot.com/2012/12/using-supportmapfragment.html

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


【解决方案1】:

这个问题通常源于您的项目缺少android-support 库。发生这种情况是因为旧版本的 api 片段不存在,因此您需要添加此库才能在旧版本的 Android 中添加对片段的支持。

为此,请右键单击您的项目,然后:

此外,您还可以阅读我写的关于在您的应用程序中实现 Google 地图的博文:

Google Maps API V2

【讨论】:

    【解决方案2】:

    我遇到了同样的问题并尝试了 2 天之类的每种可能性,但我改变了我的布局 xml 文件,并且在 java 代码中我使用了 supportmapfragment

    还可以从包资源管理器中导入 android 支持库

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

    【讨论】:

      【解决方案3】:

      我已通过在清单中添加这些行来解决此问题。

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

      【讨论】:

        【解决方案4】:

        按照link中的所有步骤进行操作

        您错过了参考 Google 地图 在你的代码中添加这个

        public class GoogleMaps extends FragmentActivity {
            private GoogleMap mMap;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_google_maps);
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                            .getMap();
            }
        
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.google_maps, menu);
                return true;
            }
        
        }
        

        除了添加 map key 外,还要在项目中添加播放服务

        【讨论】:

          【解决方案5】:

          我遇到了同样的错误,并通过不同的stackoverflow答案多次尝试解决了它:

          不确定哪个/是有助于解决问题的,但我将仅列出我所做的事情:

          • 右键Project -> Properties -> Android -> Change Project Build Target to Android 4.1.2 (Android Open Source Project) -> 在Library下,删除google-play-service-lib并重新添加->点击“应用”->点击“确定”
          • 右键单击项目 -> Android 工具 -> 修复项目属性 (by Emil Adz)
          • 在课堂上参考谷歌地图(Basavaraj Hampali

            private GoogleMap mMap;
            
            mMap = ((SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            if (mMap== null) {
                Toast.makeText(this,"Map not Available", Toast.LENGTH_LONG).show();
            }
            
          • 扩展 android.support.v4.app.FragmentActivity 而不仅仅是“FragmentActivity”(ayush1794

            extends android.support.v4.app.FragmentActivity
            

          希望对你有帮助:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-06
            • 2023-03-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多