【问题标题】:Method called inside interface method returns null在接口方法内部调用的方法返回 null
【发布时间】:2019-07-02 02:53:30
【问题描述】:

我创建了一个界面,以便我可以在对话和片段之间进行通信。

目标:当用户从对话中选择任何内容时,它应该将其显示在文本视图上。

在这个界面中,我创建了一个界面方法,在主活动中调用并传递用户在对话框中选择的值。除了用户选择的值,在我的片段中,我创建了一个方法,将文本视图设置为该值。但是,每当我调用该方法时,它总是返回 null。

我对日志进行了大量测试,发现通过我的方法传递的值不是 null,一切似乎都按照我想要的方式工作,这很奇怪。更让我困惑的是,这个方法甚至没有运行,它在执行里面的代码之前立即返回null,这对我来说真的很奇怪。

对话代码:

    public String users_time;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final String time_options[] = {"10", "20", "30", "40", "50", "60", "70", "80", "90"}; // Since we know how many options there are in the array we use an array instead of an arraylist

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("Choose the time");
        builder.setSingleChoiceItems(time_options, -1, new DialogInterface.OnClickListener() { // check items = what index is auto selected in the dialog, use -1 bc you dont want that
            @Override
            public void onClick(DialogInterface dialog, int which) { // which = Index in the array
                CharSequence time = time_options[which];
                Log.i("this" ,"LOG 1 dialogsTime retusn" + time);
                listener.onDialogInput(time);

                users_time = time_options[which];

                int usersTime = Integer.valueOf(users_time);
                listener.grabTime(usersTime);

            }
        });
        builder.setPositiveButton("Set Time", new DialogInterface.OnClickListener() { // positive = Ok or continue
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // Negative = Cancel or stop
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });


        return builder.create(); // always return this at the end
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof  DiaglogListener) {
            listener = (DiaglogListener) context;
        }
        else {
            throw new RuntimeException(context.toString()
                    + " must implement DiaglogListener");
        }
    }
    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
}

主Activity接口方法:

@Override
public void onDialogInput(CharSequence dialogsTime) {
    Fragment1_timer frag1 = new Fragment1_timer();

    Log.i("this" ,"LOG 2 runs successfully");

    try {
        frag1.setDialogTime(dialogsTime);
    } catch (Exception e){
        Toast.makeText(this, "Null error :/", Toast.LENGTH_SHORT).show();
    }
}

分片方法:

public void setDialogTime(CharSequence time){ 
    Log.i("this" ,"LOG 3 ran successfully");
    text_view_time.setText(time + ":00");
}

【问题讨论】:

  • 能贴出错误日志吗?

标签: android


【解决方案1】:

您不能使用 onAttach 方法进行 Fragment 到 DialogFragment 的通信。 您将不得不为此使用“setTargetFragment”和“getTargetFragment”。

你可以参考这个答案。 https://stackoverflow.com/a/32323822/9792247

【讨论】:

    猜你喜欢
    • 2019-12-29
    • 1970-01-01
    • 2017-05-07
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多