美好的一天,在廣播接收機(jī)中,系統(tǒng)重新啟動(dòng)后,我試圖顯示一個(gè)警報(bào)對(duì)話框。我已經(jīng)在清單中添加了接收者并調(diào)用了所需的權(quán)限,但是在顯示對(duì)話框時(shí)出現(xiàn)錯(cuò)誤。請問我該如何正確實(shí)施呢?我的代碼:public void onReceive(final Context context, Intent intent) { Log.d(TAG, "received boot completed broadcast receiver... starting settings"); String settings = context.getResources().getString(R.string.restart_setting); String yes = context.getResources().getString(R.string.Settings); String no = context.getResources().getString(R.string.Cancel); final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(settings) .setCancelable(false) .setPositiveButton(yes, new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) Intent config = new Intent(context, WeatherConfigure.class) context.startActivity(config); } }) .setNegativeButton(no, new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); }正在收到此日志錯(cuò)誤:01-07 01:42:01.559: ERROR/AndroidRuntime(2004): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application01-07 01:42:01.559: ERROR/AndroidRuntime(2004): at android.view.ViewRoot.setView(ViewRoot.java:548)01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)01-07 01:42:01.559: ERROR/AndroidRuntime(2004): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at android.app.Dialog.show(Dialog.java:288)01-07 01:42:01.559: ERROR/AndroidRuntime(2004):at com.MuaaApps.MyWeatherUpdate.myWeatherBroadcastReceiver.onReceive(MyWeatherBroadcastReceiver.java:59)
系統(tǒng)重新啟動(dòng)后,在廣播接收器中顯示警報(bào)對(duì)話框
繁華開滿天機(jī)
2019-10-29 14:50:25