如果点击任何项目两次,片段切换在滑动抽屉中崩溃?

时间:2015-02-04 17:28:23

标签: android android-fragments

我一直在使用Sliding Drawer。现在我有Home和Friends等选项。如果我首先单击HomeFragment它工作得很好然后点击FriendsFragment它也很好用。如果我再次点击HomeFragment再次从滑动抽屉它崩溃。它给出了以下错误

Caused by: java.lang.IllegalArgumentException: Binary XML file line #44: 

    Duplicate id 0x7f0a008c, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
                at android.app.Activity.onCreateView(Activity.java:4722)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at com.example.MAPit.MAPit.HomeFragment.onCreateView(HomeFragment.java:54)
                at android.app.Fragment.performCreateView(Fragment.java:1695)
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
                at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
                at android.app.BackStackRecord.run(BackStackRecord.java:682)
                at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
                at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
                at android.os.Handler.handleCallback(Handler.java:725)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5041)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                at dalvik.system.NativeStart.main(Native Method)

我在下面写了代码来切换片段

FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

然后我尝试做这个代码,但得到了与上述相同的错误。

FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container,fragment);
            transaction.commit();

我在片段的oncreateView中有什么需要做的,就像我在HomeFragment中一样吗

public class HomeFragment extends Fragment implements View.OnClickListener {
    private GoogleMap map;
    MapFragment mapFrag;
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

         View v = inflater.inflate(R.layout.home_map_activity, null, false);

        mapFrag = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        map = mapFrag.getMap();
        //added the custom info adapter
        if (map != null) {
            map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    // creating my own info for latest frnd status
                    String status="Hi how are you everyone? Have a nice day ahead";
                    View v = getActivity().getLayoutInflater().inflate(R.layout.map_info_listview, null);
                    TextView tvFrndname = (TextView) v.findViewById(R.id.tv_frnd_name);
                    TextView tvFrndStatus = (TextView) v.findViewById(R.id.tv_frnd_status);
                    tvFrndname.setText("Neerob Basak"); // Later it will be name of friend
                    //checking for length of status
                    if(status.length()>10){
                        String substatus = status.substring(0,20);
                        substatus +="...";
                        tvFrndStatus.setText(substatus);
                    }
                    return v;
                }
            });
        }

       //onclick listener on marker of friends location

        map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                FragmentManager fragmentManager = getFragmentManager();
                Fragment fragment = new FriendsStatusFragment();
                fragmentManager.beginTransaction().replace(R.id.frame_container,fragment).commit();
            }
        });

        return v;
    }

当我点击之前点击的滑动抽屉中的相同选项时,是否有必须给出的检查?

2 个答案:

答案 0 :(得分:1)

将其添加到您的片段Java文件

@Override
    public void onDestroyView() 
         {
            super.onDestroyView(); 
            Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.map);  
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }

并添加此

@Override
public void onPause() 
{
  super.onPause();
 if(map!=null)
     map=null;
}

答案 1 :(得分:1)

调用commit()后,片段事务不会立即发生。

您可以考虑在第一次单击后禁用按钮,然后在片段完成后重新启用它以防止两次启动相同的片段。这是一种称为去抖动的方法。

另一种选择是调用fragmentManager.executePendingTransactions();在您提交后立即处理该事务。