我试图让用户在点击按钮时选择一个日期并显示3个滚轮来选择日,月和年它然后将设置用户选择的任何内容到文本视图,此时我点击按钮什么都没发生只是空点击任何帮助将不胜感激。我将在下面插入代码。我收到了这行代码的错误。
fragment.show(getSupportFragmentManager(), "date");
Datepicker代码
public void datePicker(View view) {
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
public void setDate(final Calendar calendar) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
((TextView) findViewById(R.id.dateSet)).setText(dateFormat.format(calendar.getTime()));
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar cal = new GregorianCalendar(year, month, dayOfMonth);
setDate(cal);
}
public static class DatePickerFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(),
(DatePickerDialog.OnDateSetListener)
getActivity(), year, month, day);
}
public void show(FragmentManager supportFragmentManager, String date) {
}
}
}
按钮和TextView代码:
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dateSet"
android:layout_above="@+id/Category"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp" />
<Button
android:text="Pick Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/dateSet"
android:layout_alignLeft="@+id/incomeDate"
android:layout_alignStart="@+id/incomeDate"
android:layout_marginBottom="17dp"
android:onClick="datePicker"
android:id="@+id/picDate" />
错误:
Error:(135, 17) error: no suitable method found for show(android.support.v4.app.FragmentManager,String)
method DialogFragment.show(android.app.FragmentManager,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager)
method DialogFragment.show(FragmentTransaction,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)
答案 0 :(得分:1)
错误有点令人困惑,特别是如果你只关注类名并忽略他们的包。问题是支持库使用相同的类名作为本机API。要解决此问题,请仔细检查应使用支持库的类的导入。例如
import android.support.v4.app.DialogFragment
很可能你有
import android.app.DialogFragment
这是Native API中的一个类。尽管这两个类具有相同的名称,但它们是两种不同的类型,因为它们位于不同的包中。