代码之家  ›  专栏  ›  技术社区  ›  Erik Johnson

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

  •  1
  • Erik Johnson  · 技术社区  · 7 年前

    我正在尝试为对话框中的按钮设置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 回复  |  直到 7 年前
        1
  •  4
  •   AndyOS    4 年前

    你是 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) view.findViewById(R.id.DateButton);
        // set onclicklistener
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OpenDate();
    
            }
        });
     return builder.create();
    
        2
  •  0
  •   Fenil Patel    7 年前

    我用过 DialogFragment 在我的项目上,我写的 以下方式。

    这是我的 警报碎片。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();
        }
    }
    

    输出是,

    enter image description here