在非主活动中运行日期选取器对话框



我遇到了这样的问题:

2020-06-02 18:22:49.454 1829-1829/com.bartosz.piekny.lprtracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bartosz.piekny.lprtracker, PID: 1829
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
at android.app.Dialog.show(Dialog.java:330)
at com.bartosz.piekny.lprtracker.AddNote.onClick(AddNote.java:147)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

尝试在非主活动中打开日期选取器时:

context = getApplicationContext();
dateButton = (TextView) findViewById(R.id.dateTextView);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int monthNumber = monthOfYear + 1;
dateButton.setText(dayOfMonth + "." + monthNumber + "." + year);
dateTime = LocalDateTime.now().withYear(year).withMonth(monthNumber).withDayOfMonth(dayOfMonth);
mDateTimeListener.onDateTimeChanged(dateTime);
}
};
dateButton.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(context, date, mYear, mMonth, mDay).show();
}
});

有人知道我做错了什么吗:)我正在谷歌搜索它,但我发现的所有内容都与弹出窗口有关,而不是时间选择器。我想要一个非常简单的解决方案。

我刚刚想通了。而不是打电话:

context = getApplicationContext();

我需要:

context = this;

相关内容

  • 没有找到相关文章

最新更新