通过接口

时间:2016-10-14 07:56:38

标签: java android android-fragments interface

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

我对界面的使用感到困惑。尝试调试后出现如下错误,浪费大约3天来处理它仍然无法解决。任何人都可以推荐或讨论如何解决它,谢谢。

错误:

Error:(77, 5) error: method does not override or implement a method from a supertype
Error:(39, 8) error: Fragment_step_2 is not abstract and does not override abstract method onPassValueStep2() in onPassValue2
Error:(231, 32) error: method onPassValueStep1 in class Fragment_step_1 cannot be applied to given types;
required: HashMap
found: no arguments
reason: actual and formal argument lists differ in length
Error:(232, 32) error: method onPassValueStep2 in class Fragment_step_2 cannot be applied to given types;
required: HashMap
found: no arguments
reason: actual and formal argument lists differ in length
Error:(78, 5) error: method does not override or implement a method from a supertype
Error:(36, 8) error: Fragment_step_1 is not abstract and does not override abstract method onPassValueStep1() in onPassValue

Calling Fragments' HashMap to activity

主要活动:

public interface onPassValue{
    Map<Object, String> onPassValueStep1();
}

public interface onPassValue2{
    Map<Object, String> onPassValueStep2();
}

protected void onCreate(Bundle savedInstanceState) {
    ......
    btn_sendInsureInfo.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            //CALL here
            //Fragment_step_1.onPassValueStep1();
            //Fragment_step_2.onPassValueStep2();
            ......
        }
}
......

Fragment_step_1 :( xxx是活动的名称)

public class Fragment_step_1 extends Fragment implements xxx.onPassValue {
    ......
     HashMap 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");
    }*/
}

@Override
public Map<Object, String> onPassValueStep1(HashMap insureResult) {
    for (Object key : insureResult.entrySet()) {
        //System.out.println(key + " fragment_1 : " + insureResult.get(key));
        System.out.println(" fragment_1 : " + key);
        Log.e("Hashmap", 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 pingan_insure_info){
    super.onAttach(pingan_insure_info);

    /*try {
        passValueStep2 = (onPassValueStep2) xxx;
    } catch (ClassCastException e) {
        throw new ClassCastException(xxx.toString()
                + " didn't implement onPassValue");
    }*/
}

@Override
public Map<Object, String> onPassValueStep2(HashMap insureApplicantStep2){
    for (Object key : insureApplicantStep2.entrySet()) {
        System.out.println("fragment_2 : " + key);
        Log.e("Hashmap2", String.valueOf(insureApplicantStep2));
    }
    return insureApplicant2;
}

所有片段&#39; 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);

                        }
                    }
                }
            });

        }
    });

1 个答案:

答案 0 :(得分:0)

界面方法的Fragment中的签名是错误的。你声明了界面:

public interface onPassValue{
    Map<Object, String> onPassValueStep1();
}

public interface onPassValue2{
    Map<Object, String> onPassValueStep2();
}

在片段中你有公开

public Map<Object, String> onPassValueStep1(HashMap insureResult) {

Map<Object, String> onPassValueStep2(HashMap insureApplicantStep2){

您可以注意到,接口中声明的方法没有参数。

您可以在界面中更改方法声明,添加缺少的参数,或更改Fragment

中的方法