当我运行该应用程序停止工作时,我编写此代码来显示项目中的时间。但是,当我删除show方法时,按Button时程序不会显示任何内容
请帮忙
这是代码
public class AlarmFragment extends Fragment implements
TimePickerDialog.OnTimeSetListener{
private Button clockBtn;
private TextView time;
public AlarmFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_alarm, container, false);
time = view.findViewById(R.id.alarm_time);
clockBtn= view.findViewById(R.id.clock_btn);
clockBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment clock= new TimeClock();
///////////show method /////////
clock.show(getActivity().getSupportFragmentManager(),"Time Picker");
}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText("Time:" + hourOfDay +" : "+minute);
}}
这是TimeClock片段
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class TimeClock extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
编辑:-所以我更改了上面的return语句,然后它起作用了。但是AlarmFragment中的onTimeSet方法没有执行 我做到了如下:-
Activity activity = new AlarmFragment().getActivity();
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener)activity, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
答案 0 :(得分:0)
在托管Activity
的{{1}}中,您必须实现AlarmFragment
。
例如:
TimePickerDialog.OnTimeSetListener
答案 1 :(得分:0)
您得到ClassCastException
的原因是您的getActivity
不是TimePickerDialog.OnTimeSetListener
,但是您的AlarmFragment
是:
// this is where it fails, change this line, do not copy
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
您可以重构您的TimeClock
类以委派侦听器:
public class TimeClock extends DialogFragment {
private TimePickerDialog.OnTimeSetListener listener;
public void setOnTimeSetListener(TimePickerDialog.OnTimeSetListener listener) {
this.listener = listener;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Now delegates the listener to the constructor
return new TimePickerDialog(getActivity(), listener, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
并在TimeClock.show
之前设置您的监听器:
DialogFragment clock= new TimeClock();
clock.setOnTimeSetListener(this);
clock.show(getActivity().getSupportFragmentManager(), "Time Picker");