如何使用多个按钮将数据从片段传递到片段?

时间:2015-08-19 09:10:30

标签: android android-fragments bundle

我有两个片段,Fragment-1和Fragment-2,在我的片段-1中我有三个按钮,在每个按钮的监听器中我使用bundle发送数据,并尝试进入Fragment-2,但app崩溃了并获得nullpointer异常

片段-1

 rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    All_Products tf = new All_Products();
                    Bundle bundle = new Bundle();
                    bundle.putString("catagory_name", "All");

                    tf.setArguments(bundle);
                    android.app.FragmentManager fm = getActivity().getFragmentManager();
                    android.app.FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container_body, tf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();
                }
            });

            rl1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    All_Products tf = new All_Products();
                    Bundle bundle = new Bundle();


                    bundle.putString("jorel", proidslug1);


                    tf.setArguments(bundle);
                    android.app.FragmentManager fm = getActivity().getFragmentManager();
                    android.app.FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container_body, tf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();

                }
            });
            rl2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    All_Products tf = new All_Products();
                    Bundle bundle = new Bundle();
                        bundle.putString("bestseller", proidslug2);
                    tf.setArguments(bundle);
                    android.app.FragmentManager fm = getActivity().getFragmentManager();
                    android.app.FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.container_body, tf);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                    ft.commit();

                }
            });

片段-2

Bundle bundle=this.getArguments();
        cname=bundle.getString("catagory_name");
        System.out.println("all >>>>>" + cname);

        Bundle b1=this.getArguments();
        jorlenm=b1.getString("jorel");
        System.out.println("jorel>>>>>" + jorlenm);


     if(cname.equals(bundle.getString("catagory_name")))
            {
                Toast.makeText(getActivity(),cname,Toast.LENGTH_LONG).show();

            }

           else if(jorlenm.equals(bundle.getString("jorel")))
            {
                Toast.makeText(getActivity(),jorlenm,Toast.LENGTH_LONG).show();


            }

2 个答案:

答案 0 :(得分:2)

如果您点击了ImageCell按钮,那么传递给捆绑包的数据将使用密钥rl1

然后,包含密钥jorel的捆绑包中的值将为category_name。然后通过使用空值进行检查,您可能会遇到异常。

所以你必须检查它是否是null

试试这个

null

答案 1 :(得分:1)

正如@boss建议你在3个按钮发送互斥数据时需要使用空检查器。

    Bundle bundle=this.getArguments();
    cname=bundle.getString("catagory_name");
    jorlenm=bundle.getString("jorel");

 if(cname != null)
        { 
            Toast.makeText(getActivity(),cname,Toast.LENGTH_LONG).show();

        } 

       else if(jorlenm != null)
        { 
            Toast.makeText(getActivity(),jorlenm,Toast.LENGTH_LONG).show();

        }