我想显示弹出日期选择器,我使用此代码

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();


我的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>


在7.0以下的android版本上,它可以正常工作。
但是在android 7.0上我报错

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)


请帮我

最佳答案

尝试这个。

声明这些变量

private DateDialog dialog;
boolean click=false;


imgCal1方法中单击onCreate时处理此问题

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");
        }
            }
        });


之后,仅添加此

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);
        }
    }

07-27 14:32