警报对话框CalendarView NullpointException

时间:2018-05-31 21:58:24

标签: android firebase timepicker calendarview

好日子,大家好,我希望你没事 我的问题是我收到了下面所述的错误我试图创建一个警报对话框,其中有一个CalendarView和Timepicker我试图从calendarview和datepicker获取日期并将其传递给firebase云火库。
我的代码:

AlertDialog.Builder dialog= new AlertDialog.Builder(Negociation.this);
            View mView=getLayoutInflater().inflate(R.layout.dialog_negociation,null);
            dialog.setView(mView);
            final Calendar calendar = Calendar.getInstance();

            dialog.setTitle("Modifier les modalitées de livraison:");
            dialog.setNegativeButton("sortir", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).setPositiveButton("Envoyer", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    negociationprogressbar.setVisibility(View.VISIBLE);
                    CalendarView mCalendar=(CalendarView)findViewById(R.id.nouvelledateNegociation);
                    final TimePicker mTime=(TimePicker) findViewById(R.id.nouvelleheureNegociation);
                    mCalendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                        @Override
                        public void onSelectedDayChange(@NonNull CalendarView view, final int year, final int month, final int dayOfMonth) {

                            mTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                                @Override
                                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                                    calendar.set(year,month,dayOfMonth,hourOfDay,minute);
                                }
                            });

                        }
                    });
                    Map<String,Object> Negociationmap=new HashMap<>();
                    String message="Le pharmacien "+nomPh+" a proposé d'autre horaire pour la livraison concernant le formulaire envoyée le: "+Dateenvoi+"\n"+"Le jour et l'heure proposés : "+datecreation(calendar.getTime());
                    Negociationmap.put("Message",message);
                    Negociationmap.put("Validation",false);
                    Negociationmap.put("PharmacienID",User_id);
                    mFirestore.collection("Clients/"+Client+"/Negociations").add(Negociationmap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                        @Override
                        public void onSuccess(DocumentReference documentReference) {
                            Toast.makeText(Negociation.this, "Le message est envoyé", Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(Negociation.this, "Echec d'envoi", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            });
            dialog.show();


        }
    });

}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/negociationTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15sp"
            android:fontFamily="@font/nunito_light"
            android:padding="5sp"
            android:text="Choisissez le jour et l'heure préférés"
            android:textSize="20sp" />

        <CalendarView
            android:id="@+id/nouvelledateNegociation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15sp"
            android:background="@drawable/logout_button"
            android:backgroundTint="@android:color/holo_red_light"
            android:dateTextAppearance="@style/CalenderViewDateCustomText"
            android:fontFamily="@font/nunito_light"
            android:theme="@style/CalenderViewCustom"
            android:weekDayTextAppearance="@style/CalenderViewWeekCustomText" />

        <TimePicker
    android:id="@+id/nouvelleheureNegociation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/nunito_light"
    android:theme="@style/myTimePickerStyle"
    android:layout_margin="15sp"

    />
    </LinearLayout>

</ScrollView>

错误:

beginning of crash
05-31 12:35:35.316 2891-2891/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: comi.example.youyo.medicom, PID: 2891
                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CalendarView.setOnDateChangeListener(android.widget.CalendarView$OnDateChangeListener)' on a null object reference
                                                 at comi.example.youyo.medicom.Negociation$3.onClick(Negociation.java:123)
                                                 at android.view.View.performClick(View.java:5609)
                                                 at android.view.View$PerformClick.run(View.java:22259)
                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:154)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

显示我想要做的图像:

2 个答案:

答案 0 :(得分:2)

更改此行

 CalendarView mCalendar=(CalendarView)findViewById(R.id.nouvelledateNegociation);
 final TimePicker mTime=(TimePicker) findViewById(R.id.nouvelleheureNegociation);

 CalendarView mCalendar=(CalendarView)mView.findViewById(R.id.nouvelledateNegociation);
 final TimePicker mTime=(TimePicker) mView.findViewById(R.id.nouvelleheureNegociation);

findViewById方法会从Activity的contentview中找到一个视图。要从特定ViewGroup来电View.findViewById

中查找视图

答案 1 :(得分:1)

试试这个:

public Dialog onCreateDialog(Bundle onSaveInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v= inflater.inflate(R.layout.calendarpopup, null);
        final CalendarView cal= 
       (CalendarView)v.findViewById(R.id.calendarView1);