当点击我的地图碎片两次时,非法参数



当我点击包含地图的片段的按钮时,地图就会显示出来。当我再次点击它或点击另一个片段按钮,然后再次点击包含地图的片段按钮时,我的应用程序强制退出,我在LogCat中得到这个解释:

致命异常:main
android.view. view。二进制XML文件行#8:错误膨胀类片段


由: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();
}
}

问题是,你是使用相同的地图一次又一次(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();
}
}

当你替换一个片段时,它会破坏地图

使用以下代码。它正在为我工作

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;
}

相关内容

  • 没有找到相关文章

最新更新