可能是重复的问题,但是我没有找到解决方案,每当我单击TextView时,我就在Adapter类中使用日期选择器,日期选择器显示错误


  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);
它在牛轧糖上工作,但在某些设备上不工作。

当我使用


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

最佳答案

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

关于android - 适配器类中的日期选择器显示错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48824837/

10-12 16:14