我将尝试描述我的情况:
我有MyFragment extends Fragment
压倒一切onCreateView()
的 .
在MyFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, null);
//some manipulation with view
return view;
在my_fragment.xml
<LinearLayout
.....
>
<com.example.widgets.MyMapWidget
android:id="@+id/my_map"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</LinearLayout>
在MyMapWidget.java
public class MyMapWidget extends LinearLayout {
public MyMapWidget(final Context context, AttributeSet attrs) {
super(context, attrs);
((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.widget_map, this, true);
...
}
在widget_map.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Some another views-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
<!-- Some another views-->
</LinearLayout>
当我第一次展示时MyFragment
- 一切都很好。但是,如果然后我显示另一个片段(带有清理回堆栈)然后MyFragment
再显示 - 我得到了Inflate Exception
Stacktrase
android.view.InflateException: Binary XML file line #151: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at `com.***.****.fragments.MyFragment.onCreateView(MyFragment.java:78)`
第 78 行MyFragment.java
(方法onCreateView
)
View view = inflater.inflate(R.layout.my_fragment, null);
MyMapWidget
从my_fragment.xml
已解决的问题中删除。但我有一些问题:
- 为什么会发生?
- 我可以这样显示地图吗?
- 可能你知道另一种方式如何呈现地图像自己的一部分
Fragment
?
注意:我检查了类似问题的答案,但无法解决我的情况。