使用Interface将Dialog中的信息发送回Activity

时间:2016-10-04 12:15:36

标签: android

这是我的界面:

public interface TaskEditDialogListener {
    public void passTask(String taskTitle, String note);
}

这是我的Dialog Class

public class TaskEditDialog extends DialogFragment {
    private TaskEditDialogListener mListerner;

    public void setListerner(TaskEditDialogListener listerner) {
        mListerner = listerner;
    }


    @BindView(R.id.dialog_TaskEdt)
            EditText mTaskEdt;
    @BindView(R.id.dialog_NotesEdt)
            EditText mNoteEdt;

    LayoutInflater inflater;
    View v;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.task_edit_dialog, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                ButterKnife.bind(getActivity());

                String taskTitle = mTaskEdt.getText().toString();
                String note = mNoteEdt.getText().toString();
                mListerner.passTask(taskTitle,note);

            }
        }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        return builder.create();

    }
}

这是我的Activity类

public class DetailedTaskActivity extends AppCompatActivity implements TaskEditDialogListener {
    private String mTaskTitle;

    // Bind the widget
    @BindView(R.id.detailedTaskAct_TaskLabelTv)
    TextView mTaskLabelTv;
    @BindView(R.id.detailedTaskAct_TaskTv)
    TextView mTaskTv;
    @BindView(R.id.detailedTaskAct_DueDateLabelTv)
    TextView mDueDateLabelTv;
    @BindView(R.id.detailedTaskAct_DueDateTv)
    TextView mDueDateTv;
    @BindView(R.id.detailedTaskAct_NotesLabelTv)
    TextView mNotesLabelTv;
    @BindView(R.id.detailedTaskAct_NotesTv)
    TextView mNotesTv;
    @BindView(R.id.detailedTaskAct_PriorityLabelTv)
    TextView mPriorityLabelTv;
    @BindView(R.id.detailedTaskAct_PriorityTv)
    TextView mPriorityTv;
    @BindView(R.id.DetailedTaskAct_StatusLabelTv)
    TextView mStatusLabelTv;
    @BindView(R.id.detailedTaskAct_StatusTv)
    TextView mStatusTv;

    // Add delete button to Actionbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.delete_button, menu);
        return true;
    }
 @Override
    public void passTask(String taskTitle, String note) {
        TaskEditDialog dialog = new TaskEditDialog();
        dialog.setListerner(this);
        Task task = new Select().from(Task.class).where("task_title = ?", mTaskTitle)
                .executeSingle();
        task.setTaskTitle(taskTitle);
        task.setNotes(note);

        mTaskTv.setText(taskTitle);
        mNotesTv.setText(note);
    }

}

我尝试将对话框中的信息提供给Activity并更新数据库并设置textview。但是,我总是像这样

收到nullException错误
10-04 19:13:39.274 21018-21018/com.example.rubit1359.tudu E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.rubit1359.tudu, PID: 20819
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                                at com.example.rubit1359.tudu.ui.dialog.TaskEditDialog$2.onClick(TaskEditDialog.java:49)
                                                                                at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                at android.os.Looper.loop(Looper.java:194)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

请帮帮我。

1 个答案:

答案 0 :(得分:0)

该错误与从DialogFragmentActivity发送数据无关。这是由于NPE。

您未正确地将视图与ButterKnife中的TaskEditDialog绑定。它应该是这样的:

v = inflater.inflate(R.layout.task_edit_dialog, null);
ButterKnife.bind(this, v); // this refers to TaskEditDialog class object.

编辑1:

由于您使用的是DialogFragment,因此您可以直接与其父activity进行通信。

做这样的事情:

((DetailedTaskActivity)getActivity()).passTask(someData); // where passTask() is any public method in the activity.