gpt4 book ai didi

android - 在 Android 版本 7.0 上膨胀类 android.widget.DatePicker 时出错

转载 作者:行者123 更新时间:2023-11-29 02:41:17 26 4
gpt4 key购买 nike

我想显示弹出式日期选择器并且我使用了这段代码

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

我的xml文件

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

onCreate方法中点击imgCal1时处理

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

关于android - 在 Android 版本 7.0 上膨胀类 android.widget.DatePicker 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43883974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com