안녕하세요 저는 잠시 동안이 문제에 갇혀 있었어, 스택 오버플로 및 다른 사이트에서 많은 솔루션을 시도했지만 아무것도 보이지 않는다 나를 위해 일해. 여기 TimePicker.getCurrentHour() 및 .getCurrentMinute() api에서 null 반환 19
는 자바 코드 :
public void buttonClicked() {
final TimePicker time = (TimePicker) this.findViewById(R.id.time_picker);
final CheckBox repeat_daily = (CheckBox) this.findViewById(R.id.repeat_daily);
AlertDialog.Builder dialog;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog = new AlertDialog.Builder(AgendaActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
dialog = new AlertDialog.Builder(AgendaActivity.this);
}
dialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
time.clearFocus();
int hour = time.getCurrentHour();
int minute = time.getCurrentMinute();
Notification.getTime(hour, minute, repeat_daily.isChecked());
Notification.scheduleNotification(AgendaActivity.this, 1);
}
});
dialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
View pickers = getLayoutInflater().inflate(R.layout.time_picker,
null);
dialog.setView(pickers);
dialog.show();
}
그리고 여기 .XML : 그것은 반환
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TimePicker
android:id="@+id/time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:numbersBackgroundColor="@color/white"
android:numbersSelectorColor="@color/colorPrimaryDark"
android:tag="11"
android:timePickerMode="spinner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/repeat_daily"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="casual"
android:gravity="center"
android:text="Repeat Daily"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/time_picker" />
</android.support.constraint.ConstraintLayout>
이 오류입니다 : 'void android.widget.TimePicker.clearFocus()
를 가상 메서드를 호출하는 시도를 'null 객체 참조에
필요한 경우 알려주십시오. 더 많은 정보
추 신 : 예, 나중에 getHour()
을 시도했지만, api 검사를 나중에 할 것입니다. 그러나 문제의 원인이 아닙니다.
이러한 메서드는 null을 반환하지 않습니다. 그다지 멀지는 않습니다. 'time'은 null입니다. 즉, this.findViewById (R.id.time_picker)가 null을 반환합니다. 표시된 레이아웃이'Dialog'를 위해 사용하고있는'time_picker' 레이아웃이라면,'View'를 정확하게 찾을 수 없습니다. –