在android版本7.0上输入类android.widget.DatePicker时出错

时间:2017-05-10 05:08:28

标签: android widget android-dialogfragment android-datepicker datepickerdialog

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

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)

请帮帮我

1 个答案:

答案 0 :(得分:0)

试试这个。

声明这些变量

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