使用接口从ClassCastException

时间:2016-10-17 03:08:34

标签: java android android-fragments android-viewpager fragment

我想使用interface来实现从片段传递数据到包含onClick事件的activity按钮的通信。我可以看到Map可以编写在editText字段上有效的数据,但这些值不能发送到活动。它显示错误并在我触发活动onClick事件后停止。

程序是:

  1. 用户填写了editText字段
  2. 一旦editText字段聚焦,带有验证的TextWatcher将检查(如果无效,editText将不会放入HashMap并提示用户编辑,如果用户单击提交按钮,它将重新加热用户更改正确首先回答)

  3. 当用户填写所有字段并单击按钮时,片段上的值将作为Hashmap返回,并检查它是否具有空字段,并将值和putExtra()分解为下一个活动。

  4. 我对界面的使用感到困惑,尽管在解决此问题时我已经阅读了很多来源和案例。或者我可以使用其他解决方案来实现此功能吗?

    感谢您的帮助。

    Calling Fragments' Map to activity

    主要活动:

    ......
    Fragment_step_1 getHashMapStep1 = new Fragment_step_1();
    Fragment_step_2 getHashMapStep2 = new Fragment_step_2();
    
    public interface onPassValue{
        public Map<Object, String> onPassValueStep1(Map<Object, String> insureApplicant);
    }
    
    public interface onPassValue2{
        Map<Object, String> onPassValueStep2(Map<Object, String> insureApplicant2);
    }
    
    protected void onCreate(Bundle savedInstanceState) {
        ......
        btn_sendInsureInfo.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
    
                Fragment_step_1.onPassValueStep1();
                Fragment_step_2.onPassValueStep2();
                //NullPointerException on those two calling interface method
                ......
            }
    }
    ......
    

    Fragment_step_1 :( xxx是活动名称)

    public class Fragment_step_1 extends Fragment implements xxx.onPassValue {
        ......
         Map<Object, String> insureApplicant = new HashMap<>(4);
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onAttach(Context xxx){
        super.onAttach(xxx);
    
        try {
           passValue = (onPassValue) xxx;
        } catch (ClassCastException e) {
            throw new ClassCastException(pingan_insure_info.toString()
                    + " didn't implement onPassValue");
        }
        //THROW EXCEPTION ALWAYS
    }
    
    @Override
    public Map<Object, String> onPassValueStep1(Map<Object, String> insureResult) {
        for (Object key : insureResult.entrySet()) {
            //System.out.println(key + " fragment_1 : " + insureResult.get(key));
            System.out.println(" fragment_1 : " + key);
            Log.e("map", String.valueOf(insureResult));
        }
        return insureResult;
    }
        ......
    

    Fragment_step_2 :( xxx是活动名称)

    public class Fragment_step_2 extends Fragment implements xxx.onPassValue2{
    ......
    RelativeLayout correspondence;
    HashMap insureApplicant2 = new HashMap<>(3);
    
    @Override
    public void onAttach(Context xxx){
        super.onAttach(xxx);
    
        try {
            passValueStep2 = (onPassValueStep2) xxx;
        } catch (ClassCastException e) {
            throw new ClassCastException(xxx.toString()
                    + " didn't implement onPassValue");
        }
        //THROW EXCEPTION ALWAYS
    }
    
    @Override
    public Map<Object, String> onPassValueStep2(Map<Object, String> insureApplicantStep2){
        for (Object key : insureApplicantStep2.entrySet()) {
            System.out.println("fragment_2 : " + key);
            Log.e("Hashmap2", String.valueOf(insureApplicantStep2));
        }
        return insureApplicant2;
    }
    

    所有片段的editText将在editText有效后填写,并由用户输入并发送到该函数并存储在HashMap中。

    例如:(AddTextChangedListener with TextWatcher)

    residentAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    
            @Override
            public void afterTextChanged(Editable editable) {
                residentAddress.setOnFocusChangeListener(new View.OnFocusChangeListener(){
                    @Override
                    public void onFocusChange(View view, boolean isFocus){
                        if(!isFocus){
                            if("".trim().equals(residentAddress.getText().toString())){
                                rAddress.setError("Resident Address is required.");
                                strAddress = "";
                                insureApplicant2.put(2, strAddress);
                            } else {
                                rAddress.setErrorEnabled(false);
                                rAddress.setError(null);
                                strAddress = residentAddress.getText().toString().trim();
                                insureApplicant2.put(2, strAddress);
    
                                onPassValueStep2(insureApplicant2);
                                //CAN PUT THE VALUE TO HASHMAP BUT CANNOT be RETURNED TO ACTIVITY :(
                            }
                        }
                    }
                });
    
            }
        });
    

1 个答案:

答案 0 :(得分:1)

要将值从片段传递到活动,请在活动中的fragment.not中创建接口。

片段A

public class FragmentA extends Fragment {

    public interface InterfaceTest{
        void passValue(String passval);
    }

    InterfaceTest interfaceTest;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);


        interfaceTest= (InterfaceTest) context;


    }

    public FragmentA() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView= inflater.inflate(R.layout.fragment_, container, false);



        return  rootView;

    }
//
    public void passToActivity(){
        interfaceTest.passValue("yourvalues");
    }

}

活性

public class MainActivity extends AppCompatActivity implements FragmentA.InterfaceTest {

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

        //create code for add fragment in activity


    }

    @Override
    public void passValue(String passval) {
        Log.e("print",passval);
    }
}