问题
I want to show popup date picker and i using this code
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
txtDate1.setText(selectedmonth + "/" + selectedday + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
my xmlfile
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:id="@+id/lLdate1">
<TextView android:text="Tgl 1" android:textSize="15sp" android:textStyle="bold"
android:textColor="@color/black" android:layout_width="95dp" android:layout_height="wrap_content"/>
<EditText android:id="@+id/txtDate1" android:layout_width="130dp" android:layout_height="wrap_content"
android:textColor="@color/black" android:textSize="15dp" android:singleLine="true"
android:editable="false"> </EditText>
<ImageButton android:id="@+id/imgCal1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="7dp"
android:cropToPadding="true" android:src="@drawable/calendar25"/> </LinearLayout>
on android version below 7.0 it work fine. but on android 7.0 i getting error
android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class android.widget.DatePicker
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:119)
at android.app.DatePickerDialog.<init>(DatePickerDialog.java:90)
at com.MyProject.screen.FilteringFragment$3.onClick(FilteringFragment.java:244)
please help me
回答1:
Try this.
Declare these variables
private DateDialog dialog;
boolean click=false;
Handle this when imgCal1
is clicked in onCreate
method
imgCal1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
click=true;
Dialog dialogFrg=dialog.getDialog();
if(dialogFrg!=null && dialogFrg.isShowing()) {
// no need to call dialog.show(ft, "DatePicker");
} else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
After that only add this
public static class DateDialog extends android.support.v4.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
public DateDialog() {
}
public Dialog onCreateDialog(Bundle savedInstanceState) //display calender
{
final Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),this,year, month,day);
}
public void onDateSet(DatePicker view,int year, int month, int day)
{
String date1=year+"-"+String.format("%02d", month+1)+"-"+String.format("%02d", day);
date.setText(date1);
date2= date.getText().toString();
return ;
//t.add_new(date);
}
}
来源:https://stackoverflow.com/questions/43883974/error-inflating-class-android-widget-datepicker-on-android-version-7-0