2017-11-21 6 views
0

こんにちは私はしばらくこの問題に悩まされていましたが、スタックオーバーフローや他のサイトで多くのソリューションを試しましたが、私のために働く。ここでTimePicker.getCurrentHour()と.getCurrentMinute()がAPIでnullを返す19

は、Javaコードです:

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のオブジェクト参照の上に

あなたが必要かどうか教えてくださいその他の情報

PS:はい、私は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 ..Cheers !!! –

関連する問題