将数据发送到片段

时间:2016-11-07 20:52:27

标签: android android-fragments nullpointerexception

我有两个片段我想通过将颜色字符串发送到第二个片段来改变其中一个的颜色这里是我的MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public void onclickButton(String SendColor) {
    FragmentTwo fragmentTwo= (FragmentTwo) getFragmentManager().findFragmentById(R.id.second_fragment);
    fragmentTwo.changeColor(SendColor);

}}

第一个片段的代码:

public class FragmenyOne extends Fragment {


    private String color;
    private onClickColorButtonListener listener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         super.onCreateView(inflater, container, savedInstanceState);
        final View view=inflater.inflate(R.layout.activity_fragmeny_one,container,false);
        Button button= (Button) view.findViewById(R.id.btnColor);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.RG);
                int selectedId = radioGroup.getCheckedRadioButtonId();
              //  if (selectedId != 0) {
                    RadioButton RB = (RadioButton) view.findViewById(selectedId);
                    String textRB = RB.getText().toString();
                    switch (textRB) {
                        case "Red":
                            color = "Red";
                            break;
                        case "Yellow":
                            color = "Yellow";
                            break;
                        case "Blue":
                            color = "Blue";
                            break;
                    }

                        listener.onclickButton(color);
               // }
            }
        });

        return view;
    }
    public interface onClickColorButtonListener{
        void onclickButton(String SendColor);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);if(context instanceof onClickColorButtonListener)
      listener=(onClickColorButtonListener) context;

    }
}

和第二个片段的代码:

public class FragmentTwo extends Fragment {

    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.activity_fragment_two,container,false);
        return view;
    }
    public void changeColor(String color){
        int drawable= Color.WHITE;
        switch (color){
            case "Blue":
                drawable=Color.BLUE;
                break;
            case "Red":
                drawable=Color.RED;
                break;
            case "Yellow":
                drawable=Color.YELLOW;
                break;
        }
        LinearLayout li=(LinearLayout)getView().findViewById(R.id.activity_fragment_two);
        li.setBackgroundColor(drawable);
    }
}

但在播放应用程序期间的android显示错误java.lang.NullPointerException和对listener.onclickButton(颜色)的引用;在第一个片段中。

我很乐意帮助我。

0 个答案:

没有答案