首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >警报对话框CalendarView NullpointException

警报对话框CalendarView NullpointException
EN

Stack Overflow用户
提问于 2018-06-01 05:58:24
回答 2查看 295关注 0票数 0

大家好,我希望你们一切都好

我的问题是,我得到的错误陈述如下我试图创建一个警报对话框,其中有一个CalendarView和时间选择器,我试图从日历视图和日期选择器中获取日期,并将其传递到firebase云firestore。

我的代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

错误:

代码语言:javascript
复制
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)

显示我想要做的事情的图像:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-01 06:22:14

更改此行

代码语言:javascript
复制
 CalendarView mCalendar=(CalendarView)findViewById(R.id.nouvelledateNegociation);
 final TimePicker mTime=(TimePicker) findViewById(R.id.nouvelleheureNegociation);

代码语言:javascript
复制
 CalendarView mCalendar=(CalendarView)mView.findViewById(R.id.nouvelledateNegociation);
 final TimePicker mTime=(TimePicker) mView.findViewById(R.id.nouvelleheureNegociation);

findViewById方法将从Activity的contentview中查找视图。调用View.findViewById查找特定ViewGroup表单的视图

票数 2
EN

Stack Overflow用户

发布于 2018-06-01 12:23:44

试试这个:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50633372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档