0

This is my interface:

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

This is my 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();

    }
}

This is my Activity class

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);
    }

}

I try to get the information from the dialog to the Activity and update a database and set the textview. However, I always receive the nullException Error like this

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)

Please help me.

Tran Hai
  • 3
  • 1

1 Answers1

0

The error has nothing to do with sending data from DialogFragment to Activity. It's is due to NPE.

You are not correctly binding the views with ButterKnife in TaskEditDialog. It should be like this:

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

EDIT 1:

Since you are using DialogFragment, then you can directly communicate to its parent activity.

Do something like this:

((DetailedTaskActivity)getActivity()).passTask(someData); // where passTask() is any public method in the activity.
Rohit Arya
  • 6,458
  • 1
  • 21
  • 37