2017-07-12 10 views
0

私はAndroidには新しく、現在TimePickerに苦労しています。だから、私はここにTimePickerを持っている:TimePickerが完了したときに非静的関数を呼び出す

public class TimePickerFragment extends DialogFragment 
    implements TimePickerDialog.OnTimeSetListener { 

    public String time; 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 
     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 

     // Create a new instance of TimePickerDialog and return it 
     return new TimePickerDialog(getActivity(), this, hour, minute, 
      DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // Do something with the time chosen by the user 
     time = hourOfDay + ":" + minute; 
     //update global variable 
     MockDB.setCheckout(time); 
    } 
} 

この作業が、ユーザーの後、私はピッカーが、ボタンの色やテキストを変更することである活動の関数を呼び出すしたい時間を選択しています。これは、AppCompatActivityを拡張するReserveProductという関数にあります。

public void animateButtons() { 
    //picker disappears until next button is clicked 
    Button picker = (Button) findViewById(R.id.button6); 
    picker.setVisibility(View.GONE); 
    Button picker1 = (Button) findViewById(R.id.button7); 
    picker1.setVisibility(View.GONE); 
    if (settingReturn == false) { 
     //first button turns gray 
     Button bttn1 = (Button) findViewById(R.id.buttonCheckIn); 
     bttn1.setBackgroundResource(R.drawable.button_inactive); 
     String time = ((MockDB) this.getApplication()).getCheckout(); 
     bttn1.setText("Check Out: 12:27 PM"); 
     //new button appears 
     Button bttn2 = (Button) findViewById(R.id.buttonCheckOut); 
     bttn2.setVisibility(View.VISIBLE); 
     settingReturn = true; 
    } else { 
     //make 2nd button inactive 
     Button bttn2 = (Button) findViewById(R.id.buttonCheckOut); 
     bttn2.setBackgroundResource(R.drawable.button_inactive); 
     String time = ((MockDB) this.getApplication()).getReturn(); 
     bttn2.setText("Return: 1:27 PM"); 
     //show new buttons 
     Button set = (Button) findViewById(R.id.buttonSet); 
     set.setVisibility(View.VISIBLE); 
     Button home = (Button) findViewById(R.id.buttonHome); 
     home.setVisibility(View.VISIBLE); 

    } 
} 

私の問題は、この関数は静的ではないため、単にTimePickerクラスから呼び出すことができないということです。 AppCompatActivityを拡張する必要があるため、AppCompatActivityとDialogFragmentに競合するクラスがあるため、ボタンを変更する機能をTimePickerクラスに移動できません。また、findViewById()関数がエラーをスローするため、animateButtons()クラスを静的にすることもできません。

助けてください!

答えて

1

私はこれを試してみましたが、それはもうエラーを投げていないが、私は時間ピッカーで「OK」打ったとき、アプリがクラッシュされ、最初すなわち

(new SomeClass()).someMethod(); 
+0

をクラスをインスタンス化 –

関連する問題