Fragment Force Close 中的 DatePickerDialog
DatePickerDialog in Fragment Force Close
我需要你的帮助。我是 Android App 的新手,我想用 DatePickerDialog 创建一个日期选择器。我可以让对话框显示并选择日期,但是当我单击“确定”时,应用程序已关闭。我不知道我哪里出错了,我试图寻找所有的解决方案,但我仍然无法解决这个问题。
public class NotificationsFragment extends Fragment {
private NotificationsViewModel notificationsViewModel;
private DatePickerDialog datePickerDialog;
private Button lihatBtn;
private TextView tanggalDisplay, hariDisplay, bulanDisplay;
private CardView cardView;
private SimpleDateFormat simpleDateFormat;
private String hari, tanggal, bulan;
private DatePickerDialog.OnDateSetListener dateSetListener;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
notificationsViewModel =
new ViewModelProvider(this).get(NotificationsViewModel.class);
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
TextView hariDisplay = root.findViewById(R.id.text_hari_riwayat);
TextView tanggalDisplay = root.findViewById(R.id.text_tanggal_riwayat);
TextView bulanDisplay = root.findViewById(R.id.text_riwayat);
CardView cardView = root.findViewById(R.id.card_date);
Button lihatBtn = root.findViewById(R.id.lihat);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDateDialog();
}
});
return root;
}
private void showDateDialog(){
/**
* Calendar untuk mendapatkan tanggal sekarang
*/
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);
datePickerDialog.getWindow();
datePickerDialog.show();
/**
* Initiate DatePicker dialog
*/
dateSetListener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
bulan = month + " " + year;
tanggal = String.valueOf(day);
bulanDisplay.setText(bulan);
tanggalDisplay.setText(tanggal);
}
};
}
}
这是XML
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_date"
android:layout_marginTop="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_tanggal_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textSize="30sp"
android:textColor="@color/primary"
android:textStyle="bold"
android:text="27"
/>
<TextView
android:id="@+id/text_hari_riwayat"
android:layout_toEndOf="@+id/text_tanggal_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="15sp"
android:text="Sabtu"
/>
<TextView
android:id="@+id/text_bulan_riwayat"
android:layout_toEndOf="@+id/text_tanggal_riwayat"
android:layout_below="@+id/text_hari_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="15sp"
android:text="November 2020"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
这是错误日志
2020-11-29 14:22:57.243 25192-25192/com.github.otoponik E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.otoponik, PID: 25192
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.github.otoponik.ui.notifications.NotificationsFragment.onDateSet(NotificationsFragment.java:112)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:173)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
您必须在使用前实例化 dateSetListener
:
private void showDateDialog(){
dateSetListener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
bulan = month + " " + year;
tanggal = String.valueOf(day);
bulanDisplay.setText(bulan);
tanggalDisplay.setText(tanggal);
}
};
/**
* Calendar untuk mendapatkan tanggal sekarang
*/
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);
datePickerDialog.getWindow();
datePickerDialog.show();
/**
* Initiate DatePicker dialog
*/
}
我需要你的帮助。我是 Android App 的新手,我想用 DatePickerDialog 创建一个日期选择器。我可以让对话框显示并选择日期,但是当我单击“确定”时,应用程序已关闭。我不知道我哪里出错了,我试图寻找所有的解决方案,但我仍然无法解决这个问题。
public class NotificationsFragment extends Fragment {
private NotificationsViewModel notificationsViewModel;
private DatePickerDialog datePickerDialog;
private Button lihatBtn;
private TextView tanggalDisplay, hariDisplay, bulanDisplay;
private CardView cardView;
private SimpleDateFormat simpleDateFormat;
private String hari, tanggal, bulan;
private DatePickerDialog.OnDateSetListener dateSetListener;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
notificationsViewModel =
new ViewModelProvider(this).get(NotificationsViewModel.class);
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
TextView hariDisplay = root.findViewById(R.id.text_hari_riwayat);
TextView tanggalDisplay = root.findViewById(R.id.text_tanggal_riwayat);
TextView bulanDisplay = root.findViewById(R.id.text_riwayat);
CardView cardView = root.findViewById(R.id.card_date);
Button lihatBtn = root.findViewById(R.id.lihat);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDateDialog();
}
});
return root;
}
private void showDateDialog(){
/**
* Calendar untuk mendapatkan tanggal sekarang
*/
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);
datePickerDialog.getWindow();
datePickerDialog.show();
/**
* Initiate DatePicker dialog
*/
dateSetListener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
bulan = month + " " + year;
tanggal = String.valueOf(day);
bulanDisplay.setText(bulan);
tanggalDisplay.setText(tanggal);
}
};
}
}
这是XML
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_date"
android:layout_marginTop="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_tanggal_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textSize="30sp"
android:textColor="@color/primary"
android:textStyle="bold"
android:text="27"
/>
<TextView
android:id="@+id/text_hari_riwayat"
android:layout_toEndOf="@+id/text_tanggal_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="15sp"
android:text="Sabtu"
/>
<TextView
android:id="@+id/text_bulan_riwayat"
android:layout_toEndOf="@+id/text_tanggal_riwayat"
android:layout_below="@+id/text_hari_riwayat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="textStart"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="15sp"
android:text="November 2020"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
这是错误日志
2020-11-29 14:22:57.243 25192-25192/com.github.otoponik E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.otoponik, PID: 25192
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.github.otoponik.ui.notifications.NotificationsFragment.onDateSet(NotificationsFragment.java:112)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:173)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)
您必须在使用前实例化 dateSetListener
:
private void showDateDialog(){
dateSetListener = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
bulan = month + " " + year;
tanggal = String.valueOf(day);
bulanDisplay.setText(bulan);
tanggalDisplay.setText(tanggal);
}
};
/**
* Calendar untuk mendapatkan tanggal sekarang
*/
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);
datePickerDialog.getWindow();
datePickerDialog.show();
/**
* Initiate DatePicker dialog
*/
}