可能是可能的,这是重复的问题,但我找不到它的解决方案我在Adapter类中使用日期选择器每当我点击TextView,日期选择器显示错误
Unable to add window -- token null is not for an application
我已经在Stack Overflow上搜索了它我得到了解决方案,但它只在某些设备上工作,
private String dateDialouge(final BwViewHolder feedListRowHolder) {
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
feedListRowHolder.tv_date.setText(dateTime);
}
}, year, month, date);
//setDate = feedListRowHolder.tv_date.getText().toString();
datePickerDialog.getDatePicker().setMinDate(minDate);
datePickerDialog.getDatePicker().setMaxDate(maxDay); // 24 hours
datePickerDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//WindowManager.LayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
datePickerDialog.show();
return dateTime;
}
当我使用窗口类型datePickerDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
时
它正在开发Nougat,但没有在某些设备上工作。
当我使用
时
datePickerDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
它会显示错误android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application in adapter class
获取权限
<uses-permission android:name="INTERNAL_SYSTEM_WINDOW "/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
查看我的构造函数
public BoxServicesAdapter(Activity activity,Context context, List<BoxServicesBean> feedItemList, String service_id, String service_title) {
this.feedItemList = feedItemList;
this.mContext = context;
this.service_id = service_id;
this.service_title = service_title;
this.boxadapter = this;
this.activity=activity;}
答案 0 :(得分:1)
WindowManager.LayoutParams.TYPE_TOAST
对于非系统应用程序,此常量在API级别26中已弃用。请改用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
。
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
对于非系统应用,此常量在API级别26中已弃用。使用 而是
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
。
WindowManager.LayoutParams.TYPE_TOAST
需要S YSTEM_ALERT_WINDOW permission
。