尝试在DialogFragment中的按钮上设置OnClickListener

时间:2018-01-12 05:24:53

标签: android android-fragments dialog

我正在尝试为对话框中的按钮设置onclicklistener。但是,当我的对话框加载时,它会崩溃,在Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference行上显示date = (Button) v.findViewById(R.id.DateButton);。我有以下对话框:

public class EventsDialog extends DialogFragment {

    private Button date;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.event_dialog, container, false);
        date = (Button) v.findViewById(R.id.DateButton);
        // set onclicklistener
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenDate();

            }
        });
        return v;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.event_dialog, null));

        return builder.create();

    }

    public void OpenDate() {
        DatePickerFragment newFragment = new DatePickerFragment();
        newFragment.show(getActivity().getSupportFragmentManager(), "datepicker");
    }
}

布局:

android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="@dimen/mr_controller_volume_group_list_max_height"
    android:layout_height="match_parent"
    android:background="#dfdfdf"
    android:orientation="vertical">


    <TextView
        android:id="@+id/eventsHeader"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:background="@color/colorAccent"
        android:text="Add a New Event!"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:textColor="@android:color/white" />

    <EditText
        android:id="@+id/EventName"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="7dp"
        android:hint="Event Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/eventsHeader" />

    <Button
        android:id="@+id/DateButton"
        android:layout_width="0dp"
        android:layout_height="36dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="10dp"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/EventName" />

    <Button
        android:id="@+id/TimeButton"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="10dp"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DateButton" />

    <EditText
        android:id="@+id/EventLocation"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="10dp"
        android:hint="Location"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/TimeButton" />

    <EditText
        android:id="@+id/EventDetails"
        android:layout_width="0dp"
        android:layout_height="186dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="Details"
        android:inputType="textMultiLine"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/EventLocation" />

    <ImageButton
        android:id="@+id/SaveButtonEvents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/EventDetails"
        app:srcCompat="@android:drawable/ic_menu_save" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:2)

inflating两次layout。请使用onCreateDialog()方法对其进行充气,然后执行以下操作并删除onCreateView()方法:

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

    View view = getActivity().getLayoutInflater().inflate(R.layout.event_dialog, null,false);

    builder.setView(view);
    builder.setTitle("Your Title");

    date = (Button) v.findViewById(R.id.DateButton);
    // set onclicklistener
    date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            OpenDate();

        }
    });
 return builder.create();

答案 1 :(得分:0)

  

我在我的项目中使用了enter image description here,并且我已经编写了它   以下方式。

这是我的 AlertDFragment.Java

public class AlertDFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                // Set Dialog Icon
                .setIcon(R.drawable.androidhappy)
                // Set Dialog Title
                .setTitle("Alert DialogFragment")
                // Set Dialog Message
                .setMessage("Alert DialogFragment Tutorial")

                // Positive button
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something else
                    }
                })

                // Negative Button
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something else
                    }
                }).create();
    }
}

输出是,

{{3}}