当我点击包含地图的片段的按钮时,地图会显示。当我再次单击它或单击另一个片段按钮,然后再次单击包含该地图的片段的按钮时,我的应用程序强制退出并在LogCat中得到此解释:
致命异常:主要是:android.view.InflateException:BINARY XML文件行#8:错误扩充类片段
...
导致by:java.lang.IllegalArgumentException:二进制XML文件行:#8:复制id 0x7f070053,标记为null或父ID为0xffffffff,另一个片段为com.google.android.gms.maps.MapFragment
怎么办?我猜这可能与片段事务管理器有关,但我真的不知道?
Fragment01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
Fragment01.java
public class Fragment01 extends Fragment {
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_01, container, false);
return v;
}
@Override
public void onDestroy()
{
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
@Override
public void super.onDestroyView()
{
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
@Override
public void onDestroyView()
{
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
StartFragment startfragment = new StartFragment();
transaction.add(R.id.fragment_placeholder, startfragment);
transaction.commit();
}
public void onSelectFragment(View view){
Fragment newFragment;
if (view == findViewById(R.id.btnomforetaget)) {
newFragment = new StartFragment();
} else if (view == findViewById(R.id.btnhittahit)) {
newFragment = new Fragment01();
} else if (view == findViewById(R.id.btnhemsida)) {
newFragment = new Fragment02();
} else {
newFragment = new StartFragment();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_placeholder, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
答案 0 :(得分:3)
问题在于您在更改片段后不会一次又一次地使用相同的地图(Duplicate id 0x7f070053
)。
<强>溶液强>
public class Fragment01 extends Fragment {
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_01, container, false);
return v;
}
@Override
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
当你替换片段
时会破坏地图答案 1 :(得分:0)
使用以下代码。这对我有用
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}