2 ssummeraw ssummeraw 于 2013.06.06 10:57 提问

alert builder 中的 Token 不为空

我使用一个 receiver 每周来创建一个 alertbox。但是确获得错误:

Unable to add window -- token null is not for an application

代码如下:

public class AlarmReceiver extends BroadcastReceiver 
    {
         @Override
         public void onReceive(Context context, Intent intent) 
         {
           try {
                 displayAlert("Have you seen your chiropractor this month?", "Alert!", context);
                } 
           catch (Exception e) 
            {
               Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
                 e.printStackTrace();
              }
        }
         public void displayAlert(final String error, String title, final Context context)
         {
                new AlertDialog.Builder(context.getApplicationContext()).setMessage(error)  
                .setTitle(title)  
                .setCancelable(true)  
                .setNeutralButton("Continue",  
                   new DialogInterface.OnClickListener() {  
                   public void onClick(DialogInterface dialog, int whichButton){
                           dialog.cancel();
                            Intent newIntent = new Intent(context, Appointment.class);
                            context.startActivity(newIntent);
                   }  
                   })  
                .show(); 
            }
    }

2个回答

xiaoyan_12
xiaoyan_12   2013.06.06 13:23
已采纳

我创建了一个blank activity MYExtraActivity,然后在 Receiver 中调用

Intent newIntent = new Intent(context, MYExtraActivity .class);
                            context.startActivity(newIntent);
cj6585256
cj6585256   2013.06.06 12:49

context.getApplicationContext() 不需要使用这个 直接context就行了 Dialog是依托于Activity的生命周期,不是ApplicationContext

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!