两个onclicks被触发导致片段事务导致错误

时间:2014-12-25 06:06:07

标签: android fragmenttransaction

目前正在发生什么

  1. 我在适配器中有2个实体(线性布局),点击它 使片段事务成为新片段
  2. 这两个元素都会导致新片段
  3. 如果我点击一个班轮布局,它的工作正常。但是当我在做的时候 测试,我注意到,如果我点击它们两个错误(因为a row有两个点击导致相同的片段)两个提交不能 正如错误所暗示的那样工作
  4. 在某种程度上我可以解决这个问题,保持我的架构完整

  5. AdptHome.java

    public class AdptHome extends BaseAdapter{
    
        ArrayList<HashMap<String, String>> listData;
        Context context;
        FragmentTransaction ft;
    
        public AdptHome(ArrayList<HashMap<String, String>> _listData, Context _context, FragmentTransaction _ft) {
            // TODO Auto-generated constructor stub
            listData=_listData;
            context=_context;
            ft=_ft;
        }
    
        @Override
        public int getCount() {
            return listData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View retval;
            HashMap<String, String> mapData=listData.get(position);
    
            LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            retval = mInflater.inflate(R.layout.adpt_home_list, null);
    
            final TextView titleLeftId = (TextView) retval.findViewById(R.id.titleLeftId);
            final ImageView imageLeftId=(ImageView) retval.findViewById(R.id.imageLeftId);
            final TextView categoryLeftId=(TextView) retval.findViewById(R.id.categoryLeftId);
    
    
            final TextView titleRightId = (TextView) retval.findViewById(R.id.titleRightId);
            final ImageView imageRightId=(ImageView) retval.findViewById(R.id.imageRightId);
            final TextView categoryRightId=(TextView) retval.findViewById(R.id.categoryRightId);
    
            final LinearLayout layoutLeftId=(LinearLayout) retval.findViewById(R.id.layoutLeftId);
            final LinearLayout layoutRightId=(LinearLayout) retval.findViewById(R.id.layoutRightId);
    
            String[] imageSplitted = mapData.get("imagename").split("~");
            String[] idSplitted = mapData.get("id").split("~");
            String[] nameSplitted = mapData.get("name").split("~");
    
            //First image
            String uri1 = "@drawable/"+imageSplitted[0];
            int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
            Drawable res1 = context.getResources().getDrawable(imageResource1);
    
    
            //Second image
            String uri2 = "@drawable/"+imageSplitted[1];
            int imageResource2 = context.getResources().getIdentifier(uri2, null, context.getApplicationContext().getPackageName());
            Drawable res2 = context.getResources().getDrawable(imageResource2);
    
            imageLeftId.setImageDrawable(res1);
            titleLeftId.setText(nameSplitted[0]);
            categoryLeftId.setText(idSplitted[0]);
    
    
            imageRightId.setImageDrawable(res1);
            titleRightId.setText(nameSplitted[1]);
            categoryRightId.setText(idSplitted[1]);
    
    
    
            layoutLeftId.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleLeftId.getText().toString());  
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();
                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryLeftId.getText().toString()));
                }
            });
    
    
            layoutRightId.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    //Changing a fragment from within a displayed fragment to a new fragment
                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleRightId.getText().toString());  
                    //ft.hide(FrgHome.this);
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();
    
                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryRightId.getText().toString()));
                }
            });
    
    
    
            return retval;
        }
    
    }
    

    adpt_home_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:id="@+id/layoutLeftId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="#FFFFFF"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/imageLeftId"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                android:src="@drawable/android" />
    
            <TextView
                android:id="@+id/titleLeftId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/white"
                android:gravity="center"
                android:textColor="#000000" />
    
            <TextView
                android:id="@+id/categoryLeftId"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:visibility="invisible" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:gravity="center|left"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="32dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:background="@drawable/sel_just_a_line" />
    
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/dot" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:background="@drawable/sel_just_a_line" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/layoutRightId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:background="#FFFFFF"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/imageRightId"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:scaleType="centerCrop"
                android:src="@drawable/android" />
    
            <TextView
                android:id="@+id/titleRightId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/white"
                android:gravity="center"
                android:textColor="#000000" />
    
            <TextView
                android:id="@+id/categoryRightId"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:visibility="invisible" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:gravity="center|left"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="165dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:background="@drawable/sel_just_a_line" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/dot" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:background="@drawable/sel_just_a_line" />
        </LinearLayout>
    
    </LinearLayout>
    

    日志:

    12-25 11:23:27.563: E/AndroidRuntime(26031): FATAL EXCEPTION: main
    12-25 11:23:27.563: E/AndroidRuntime(26031): Process: com.windhyaworks, PID: 26031
    12-25 11:23:27.563: E/AndroidRuntime(26031): java.lang.IllegalStateException: commit already called
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:583)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.app.BackStackRecord.commit(BackStackRecord.java:575)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at com.windhyaworks.adapters.AdptHome$1.onClick(AdptHome.java:106)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.view.View.performClick(View.java:4456)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.view.View$PerformClick.run(View.java:18465)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.os.Handler.handleCallback(Handler.java:733)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.os.Handler.dispatchMessage(Handler.java:95)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.os.Looper.loop(Looper.java:136)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at android.app.ActivityThread.main(ActivityThread.java:5086)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at java.lang.reflect.Method.invoke(Method.java:515)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    12-25 11:23:27.563: E/AndroidRuntime(26031):    at dalvik.system.NativeStart.main(Native Method)
    

    {编辑}

    public class AdptHome extends BaseAdapter {
    
        ArrayList<HashMap<String, String>> listData;
        Context context;
        FragmentManager fm;
    
        public AdptHome(ArrayList<HashMap<String, String>> _listData,
                Context _context, FragmentManager fragmentManager) {
            // TODO Auto-generated constructor stub
            listData = _listData;
            context = _context;
            fm = fragmentManager;
        }
    
        @Override
        public int getCount() {
            return listData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View retval;
            HashMap<String, String> mapData=listData.get(position);
    
            LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            retval = mInflater.inflate(R.layout.adpt_home_list, null);
    
            final TextView titleLeftId = (TextView) retval.findViewById(R.id.titleLeftId);
            final ImageView imageLeftId=(ImageView) retval.findViewById(R.id.imageLeftId);
            final TextView categoryLeftId=(TextView) retval.findViewById(R.id.categoryLeftId);
    
    
            final TextView titleRightId = (TextView) retval.findViewById(R.id.titleRightId);
            final ImageView imageRightId=(ImageView) retval.findViewById(R.id.imageRightId);
            final TextView categoryRightId=(TextView) retval.findViewById(R.id.categoryRightId);
    
            final LinearLayout layoutLeftId=(LinearLayout) retval.findViewById(R.id.layoutLeftId);
            final LinearLayout layoutRightId=(LinearLayout) retval.findViewById(R.id.layoutRightId);
    
            String[] imageSplitted = mapData.get("imagename").split("~");
            String[] idSplitted = mapData.get("id").split("~");
            String[] nameSplitted = mapData.get("name").split("~");
    
            //First image
            String uri1 = "@drawable/"+imageSplitted[0];
            int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
            Drawable res1 = context.getResources().getDrawable(imageResource1);
    
    
            //Second image
            String uri2 = "@drawable/"+imageSplitted[1];
            int imageResource2 = context.getResources().getIdentifier(uri2, null, context.getApplicationContext().getPackageName());
            Drawable res2 = context.getResources().getDrawable(imageResource2);
    
            imageLeftId.setImageDrawable(res1);
            titleLeftId.setText(nameSplitted[0]);
            categoryLeftId.setText(idSplitted[0]);
    
    
            imageRightId.setImageDrawable(res1);
            titleRightId.setText(nameSplitted[1]);
            categoryRightId.setText(idSplitted[1]);
    
    
    
            layoutLeftId.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    FragmentTransaction ft=fm.beginTransaction();
                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleLeftId.getText().toString());  
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();
                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryLeftId.getText().toString()));
                }
            });
    
    
            layoutRightId.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    //Changing a fragment from within a displayed fragment to a new fragment
                    FragmentTransaction ft=fm.beginTransaction();
    
                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleRightId.getText().toString());  
                    //ft.hide(FrgHome.this);
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();
    
                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryRightId.getText().toString()));
                }
            });
    
    
    
            return retval;
        }
    }
    

    日志:

    12-25 11:58:04.977: E/AndroidRuntime(29100): FATAL EXCEPTION: main
    12-25 11:58:04.977: E/AndroidRuntime(29100): Process: com.windhyaworks, PID: 29100
    12-25 11:58:04.977: E/AndroidRuntime(29100): android.view.InflateException: Binary XML file line #349: Error inflating class fragment
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at com.windhyaworks.fragments.FrgAddNewJobs.onCreateView(FrgAddNewJobs.java:129)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.Fragment.performCreateView(Fragment.java:1700)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.os.Handler.handleCallback(Handler.java:733)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.os.Handler.dispatchMessage(Handler.java:95)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.os.Looper.loop(Looper.java:136)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.ActivityThread.main(ActivityThread.java:5086)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at java.lang.reflect.Method.invoke(Method.java:515)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at dalvik.system.NativeStart.main(Native Method)
    12-25 11:58:04.977: E/AndroidRuntime(29100): Caused by: java.lang.IllegalArgumentException: Binary XML file line #349: Duplicate id 0x7f0c001a, tag null, or parent id 0x7f0c0019 with another fragment for com.google.android.gms.maps.MapFragment
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.app.Activity.onCreateView(Activity.java:4808)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    12-25 11:58:04.977: E/AndroidRuntime(29100):    ... 21 more
    

2 个答案:

答案 0 :(得分:1)

不要全局创建片段事务。每次都必须创建新的片段事务。

答案 1 :(得分:0)

分享我的解决方案::

实际上问题已经创建,因为我在适配器中有两个项目,解决这个问题是设置一个会话变量。它可能不是fesable,但它是解决问题的好方法,因为它在许多地方使用


public class AdptHome extends BaseAdapter {

    ArrayList<HashMap<String, String>> listData;
    Context context;
    FragmentManager fm;

    public AdptHome(ArrayList<HashMap<String, String>> _listData,
            Context _context, FragmentManager fragmentManager) {
        // TODO Auto-generated constructor stub
        listData = _listData;
        context = _context;
        fm = fragmentManager;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View retval;
        HashMap<String, String> mapData=listData.get(position);

        LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        retval = mInflater.inflate(R.layout.adpt_home_list, null);

        final TextView titleLeftId = (TextView) retval.findViewById(R.id.titleLeftId);
        final ImageView imageLeftId=(ImageView) retval.findViewById(R.id.imageLeftId);
        final TextView categoryLeftId=(TextView) retval.findViewById(R.id.categoryLeftId);


        final TextView titleRightId = (TextView) retval.findViewById(R.id.titleRightId);
        final ImageView imageRightId=(ImageView) retval.findViewById(R.id.imageRightId);
        final TextView categoryRightId=(TextView) retval.findViewById(R.id.categoryRightId);

        final LinearLayout layoutLeftId=(LinearLayout) retval.findViewById(R.id.layoutLeftId);
        final LinearLayout layoutRightId=(LinearLayout) retval.findViewById(R.id.layoutRightId);

        String[] imageSplitted = mapData.get("imagename").split("~");
        String[] idSplitted = mapData.get("id").split("~");
        String[] nameSplitted = mapData.get("name").split("~");

        //First image
        String uri1 = "@drawable/"+imageSplitted[0];
        int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
        Drawable res1 = context.getResources().getDrawable(imageResource1);


        //Second image
        String uri2 = "@drawable/"+imageSplitted[1];
        int imageResource2 = context.getResources().getIdentifier(uri2, null, context.getApplicationContext().getPackageName());
        Drawable res2 = context.getResources().getDrawable(imageResource2);

        imageLeftId.setImageDrawable(res1);
        titleLeftId.setText(nameSplitted[0]);
        categoryLeftId.setText(idSplitted[0]);


        imageRightId.setImageDrawable(res1);
        titleRightId.setText(nameSplitted[1]);
        categoryRightId.setText(idSplitted[1]);



        layoutLeftId.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(AppController.isHorizontalListTransaction()==false){

                    AppController.setHorizontalListTransaction(true);

                    FragmentTransaction ft=fm.beginTransaction();
                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleLeftId.getText().toString());  
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();
                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryLeftId.getText().toString()));
                }
            }
        });


        layoutRightId.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(AppController.isHorizontalListTransaction()==false){

                    AppController.setHorizontalListTransaction(true);
                    //Changing a fragment from within a displayed fragment to a new fragment
                    FragmentTransaction ft=fm.beginTransaction();

                    FrgAddNewJobs fragment2 = FrgAddNewJobs.newInstance(titleRightId.getText().toString());  
                    //ft.hide(FrgHome.this);
                    ft.replace(R.id.frame_container, fragment2, FrgAddNewJobs.class.getSimpleName()).addToBackStack(FrgAddNewJobs.class.getSimpleName()).commit();

                    //Set the categoryId in the application class
                    AppController.setCategoryId(Integer.valueOf(categoryRightId.getText().toString()));
                }
            }
        });     

        return retval;
    }
}

note :: AppController是会话变量 你还必须在onclick带你的类中设置标志....简单的标志约定