2017-11-21 2 views
0

안녕하세요 저는 잠시 동안이 문제에 갇혀 있었어, 스택 오버플로 및 다른 사이트에서 많은 솔루션을 시도했지만 아무것도 보이지 않는다 나를 위해 일해. 여기 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 검사를 나중에 할 것입니다. 그러나 문제의 원인이 아닙니다.

+0

이러한 메서드는 null을 반환하지 않습니다. 그다지 멀지는 않습니다. 'time'은 null입니다. 즉, this.findViewById (R.id.time_picker)가 null을 반환합니다. 표시된 레이아웃이'Dialog'를 위해 사용하고있는'time_picker' 레이아웃이라면,'View'를 정확하게 찾을 수 없습니다. –

답변

0

비정상 배치 된 뷰를 올바르게 캐스팅하지 않으므로 null 값이 반환됩니다. 이렇게하면

public void ButtonClick() 
{ 

    AlertDialog.Builder dialog=new AlertDialog.Builder(this); 
    LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View pickers=layoutInflater.inflate(R.layout.time_picker,null); 
    final TimePicker time = (TimePicker)pickers.findViewById(R.id.time_picker); 
    final CheckBox repeat_daily = (CheckBox) pickers.findViewById(R.id.repeat_daily); 
    dialog.setView(pickers); 
    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(); 
     } 
    }); 

    AlertDialog alertDialog=dialog.create(); 
    alertDialog.show(); 

} 

잘 작동합니다.

+0

완벽하게 작동했습니다. 지식 부족으로 유감스럽게 생각합니다. 아직 첫 번째 안드로이드 프로젝트입니다. 건배! –

+0

아무 것도 배우지 못하면 오류가 친구로 간주됩니다. D .. Chee !!! –