0

毎週、レシーバーを使用してアラートボックスを作成しています。ただし、以下のコードで次のエラーが発生します

ウィンドウを追加できません-トークンnullはアプリケーション用ではありません

私が使用しているコードは

 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(); 
            }
    }
4

4 に答える 4

0

問題は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(); 

getApplicationContext()パスの代わりにActivityName.thisまたはgetContext()

于 2013-01-06T09:01:11.953 に答える
0

これの代わりに:

context.getApplicationContext();

使用する

this

そしてもう一度やり直してください、これはうまくいくはずです

于 2013-01-06T09:01:53.377 に答える
0

AlertDialogからを表示するのは良い考えだとは思いませんBroadcastReceiver。代わりにを作成し、Notificationユーザーがクリックしたときにアクティビティを開く必要があります。

于 2013-01-06T09:11:27.500 に答える
0

通知はいらない、安っぽいトリックをしただけだ

空のアクティビティ MYExtraActivity を作成し、Receiver で呼び出しました

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

この新しいアクティビティでは、アラートとすべてを定義しました

于 2013-01-06T09:49:42.217 に答える