设置Button onClickListener时的Null Object引用

时间:2015-11-01 17:23:24

标签: java android android-dialogfragment dialogfragment

我在自定义DialogFragment中有一个按钮,我试图设置OnClickListener。 DialogFragment使用包含按钮的自定义布局。当我尝试设置按钮的OnClickListener时(我说按钮为空),我收到NullPointerException。

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/task_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter a task." />

    <EditText
        android:id="@+id/task_notes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter a note." />

    <Button
        android:id="@+id/date_picker_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose a date"/>

    <TextView
        android:id="@+id/chosen_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/time_picker_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose a time"/>

    <TextView
        android:id="@+id/chosen_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

自定义DialogFragment:

public class AddTaskDialogFragment extends DialogFragment implements DatePickerFragment.setDate{

    private static final String TAG = "AddTaskDialogFragment";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.dialog_add_task, null))
                .setTitle("Create your task.")
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Task task = new Task();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                });

        AlertDialog alertDialog = builder.create();

        Button datePickerButton = (Button) alertDialog.findViewById(R.id.date_picker_button);
        // datePickerButton is null here vvvv, causing NullPointerException
        datePickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerFragment datePickerFragment = new DatePickerFragment();
                datePickerFragment.show(getFragmentManager(), "datePickerFragment");
            }
        });

        return alertDialog;

    }

}

0 个答案:

没有答案