我正在使用DatePicker
的{{1}} fragments
之一TabSwipeActivity
,其中包含3个标签。我在第二个片段中有button
“onclick”ed,调用{1}}方法,如Android Developer网站所示。
参考此链接 Pablo发布了ClassCastException fragment android using ActionBar,因为他也遇到了类似的困难。
根据Pablo在6月9日发表的最新评论,他暗示他showDatePickerDialog
没有implement Listener
。我按照他的方式和我的DatePickerFragment
移除了DatePickerFragment
部分,如下所示
listener
Pablo说他有一个调用public class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
}
}
对话框(在我的第二个片段中)的片段实现了监听器接口和监听器接口的DatePicker
方法。因此,与日期选择器相关的第二个片段类代码如下: -
onDateset
我收到此错误。
public class CaptureFrag2 extends Fragment implements DatePickerDialog.OnDateSetListener {
//showDatePickerDialog is defined in the onClick attribute corresponding to btn_dob of Fragment 2
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
String dob;
dob = String.valueOf(monthOfYear)+ "-" + String.valueOf(year)
+ "-" + String.valueOf(dayOfMonth);
btn_dob.setText(dob);
}
错误的可能区域
它在Tabs中查找java.lang.IllegalStateException: Could not find a method showDatePickerDialog(View) in the activity class com.androidexample.seetha.myapplication.Tabs for onClick handler on view class android.widget.Button with id 'capture_frag2_btn_dob'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
,它是实现showDatePickerDialog(View)
的主要活动,而在我的情况下,我在CaptureFrag2中有这个方法(即Tab活动的第二个片段)。
我应该在标签中实施TabListener
吗?如果从Tab Swipe的一个片段中实现OnDateSetListener
Dialog的人共享所有3个类中的相关代码,将会有所帮助
Tabs(实现TabListener的选项卡) CaptureFrag2(片段2) DatePickerFragment
...谢谢
答案 0 :(得分:0)
您的活动是否实施
DatePickerDialog.OnDateSetListener
否则演员
(DatePickerDialog.OnDateSetListener)getActivity()
会失败。
修改强>
我的一个片段中的一些示例代码:
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
createFancyDateTimePicker(TIME_DIALOG_ID).show();
}
};
// Timepicker dialog generation
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateTime();
}
};
protected Dialog createFancyDateTimePicker(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(getActivity(),
mDateSetListener,
mYear, mMonth, mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(getActivity(),
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
如果你想在你的onclick方法调用中显示对话框:
startCalendar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PRESSED_CALENDAR = START_TIME;
createFancyDateTimePicker(DATE_DIALOG_ID).show();
}
});
private void updateTime() {
startTime.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mHour).append(" : ")
.append(mMinute).append(" , ")
.append(mDay).append(".")
.append(mMonth + 1).append(".")
.append(mYear).append(""));
}