0

I am using a receiver to make an alertbox every week. However i get following error with the code below

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

The code I am using is

 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(); 
            }
    }
cjds
  • 8,268
  • 10
  • 49
  • 84
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

4 Answers4

0

The problem is in getApplicationContext()

new AlertDialog.Builder(this).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(); 

in place of getApplicationContext() pass ActivityName.this or getContext()

cjds
  • 8,268
  • 10
  • 49
  • 84
  • can't do any Activity.this, its a broadcastreceiver, i tried with context only but failed – Muhammad Umar Jan 06 '13 at 09:02
  • Try only passing context. i.e. `new AlertDialog.Builder(context)` . And if compiler doesn't accept that convert the context to an Activity – cjds Jan 06 '13 at 09:04
  • @MuhammadUmar This should also help http://www.vogella.com/articles/AndroidNotifications/article.html – cjds Jan 06 '13 at 09:18
0

Instead of this:

context.getApplicationContext();

use

this

And try again, this should work

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
0

I don't really think that it is a good idea to show an AlertDialog from your BroadcastReceiver. You should create a Notification instead and open some activity when user click it.

Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
0

I don't want notifications, I just did a cheap trick

I have created a blank activity MYExtraActivity and called it in Receiver

Intent newIntent = new Intent(context, MYExtraActivity .class);
                            context.startActivity(newIntent);

In this new Activity i have defined an alert and everything

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193