我正在尝试在FrameLayout
内显示GoogleMaps。这个FrameLayout
应该根据切换按钮上下移动。但是,当我尝试给视图充气时,我的应用程序崩溃了。
甚至可能我在这里做什么?我的意思是在FrameLayout
内显示GoogleMaps?我怎样才能做到这一点?
谢谢!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/fragment_a"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/lv_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:text="Hello World!"/>
</RelativeLayout>
<!-- this frame is invisible -->
<FrameLayout
android:id="@+id/fragment_b"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#FF0000">
<fragment
android:id="@+id/map_v2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>
</LinearLayout>
我的Fragment
课程中的相应代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_favorites, container, false);
final FragmentManager fm = getFragmentManager();
final RelativeLayout fragmentA = (RelativeLayout) rootView.findViewById(R.id.fragment_a);
fragmentA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout fragmentA = (RelativeLayout) getActivity().findViewById(R.id.fragment_a);
FrameLayout fragmentB = (FrameLayout)getActivity().findViewById(R.id.fragment_b);
int y = 200;
if (state == true) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
fragmentA.animate().translationY(-y).setDuration(500);
fragmentB.animate().translationY(-y).setDuration(500);
state = false;
}
} else {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
fragmentA.animate().translationY(y).setDuration(500);
fragmentB.animate().translationY(y).setDuration(500);
state = true;
}
}
}
});
return rootView;
}
我得到的错误:
04-19 15:39:43.248 8758-8758/com.mahlzeit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mahlzeit, PID: 8758
android.view.InflateException: Binary XML file line #20: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at com.mahlzeit.mahlzeitactivity.FavoritesFragment.onCreateView(FavoritesFragment.java:27)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
答案 0 :(得分:1)
看起来你正在嵌套片段。你不能拥有&lt;片段/&gt;片段布局中的标签。
点击此处: Fragments within Fragments
在片段中使用getChildFragmentManager()动态添加片段。