我已经为 ListView
header 创建了一个 XML 布局,并且还创建了一个扩展 RelativeLayout
的类来操作其元素。
其中一个元素是 GoogleMap
fragment 。
类初始化基本上是:
public class HeaderView extends RelativeLayout {
public HeaderView(final Context context, final MyData data) {
super(context);
// Line #8 - inflation error
View.inflate(context, R.layout.view_header, this);
// Get view elements, set text, image, etc.
// Can't use getSupportFragmentManager() because class is not FragmentActivity
// How can I get the map then?
GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
传递的上下文是 Activity 的基本上下文 (this.getBaseContext()
)。
实例化此类会导致错误:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
我已经将所需的元数据添加到 list 文件中,即使因为我在不同的类中使用了另一个映射(顺便说一句,它扩展了 FragmentActivity
,所以这里的映射没有问题)。
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_api_key"/>
所以基本上,这是一个二合一的问题,如何正确地膨胀 View 以及如何获取对 map fragment 的引用?
这是 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"
android:background="@color/white">
<fragment
android:layout_width="wrap_content"
android:layout_height="65dp"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<com.android.volley.toolbox.NetworkImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/seller_image"
android:scaleType="fitCenter"
android:layout_below="@+id/map"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp" />
</RelativeLayout>
最佳答案
您是否尝试过将 Activity
作为参数传递给构造函数并调用 getSupportFragmentManager()
?
关于java - 使用 ViewGroup 类中的 SupportMapFragment 来膨胀 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643623/