【问题标题】:E/AndroidRuntime(8592): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragmentE/AndroidRuntime(8592):原因:android.view.InflateException:二进制 XML 文件第 2 行:膨胀类片段时出错
【发布时间】:2014-04-17 15:34:41
【问题描述】:

好吧,我正在尝试将谷歌地图添加到我的应用程序中,但我遇到了这些错误

04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
 04-17 16:25:07.169: E/AndroidRuntime(8592): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.guide_oran-1.apk]

我的主要活动:

package com.example.guide_oran;

import android.app.Activity;
import android.os.Bundle;

public class Activity_map extends Activity{

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

我的 xml

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

【问题讨论】:

    标签: android eclipse google-maps android-fragments


    【解决方案1】:

    这可能是由您的 Activity 扩展和您的布局引起的,该布局只有 fragment 类。您需要使用FragmentActivity 扩展您的活动以允许活动处理片段。话虽如此,您还需要更改您的片段类以支持SupportMapFragment

    public class Activity_map extends FragmentActivity {
        GoogleMap googleMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.gmap);
           // find the fragment and commit it into this activity
           SupportMapFragment mapfragment = (SupportMapFragment) 
                        getSupportFragmentManager().findFragmentById(R.id.map);
           // initialization of googlemap
           googleMap = mapfragment.getMap();
    
           // It might be resumed by:
           // googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        }
    }  
    

    那么你需要将片段的class改为:

    android:name="com.google.android.gms.maps.SupportMapFragment"
    

    您可以在以下位置查看针对同一问题的多个提示:Unable instantiate android.gms.maps.MapFragment
    另外,找a complete and detailed answer使用谷歌地图,一步一步来。

    【讨论】:

      【解决方案2】:

      尝试将您的片段放入布局中:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
         <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" />
      
      </RelativeLayout>
      

      Here你可以找到一个很好的教程

      【讨论】:

      • 实际问题是他使用的是Activity 而不是FragmentActivity(如下@Filo 所指出的)
      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 2016-11-16
      • 2014-05-21
      • 2013-11-21
      • 2022-01-17
      • 2014-08-13
      • 2015-02-08
      • 1970-01-01
      相关资源
      最近更新 更多