2013-01-06 20 views
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(); 
      } 
    } 
+0

_但是,我得到下面的錯誤代碼_你得到什麼錯誤? –

+0

@PradeepSimha的第一行檢查出來,無法添加窗口 –

回答

0

我不想通知,我只是做了一個把戲

我創建了一個空白的活動MYExtraActivity並把它稱爲接收機

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

在這種新的活動我已經定義了警報和一切

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.thisgetContext()

+0

不能做任何Activity.this,它是一個broadcastreceiver,我只用上下文嘗試過但失敗 –

+0

嘗試只傳遞上下文。即'新的AlertDialog.Builder(上下文)'。並且如果編譯器不接受將上下文轉換爲活動 – cjds

+0

上下文也不起作用 –

0

的這個代替:

context.getApplicationContext(); 

使用

this 

再試,這應該工作

+0

「this」是一個broadcastReceiver –

+0

@MuhammadUmar,嘗試ActivityName.this –

+0

沒有類型MYACTIVITY的封閉實例可以在範圍內訪問 –

0

我真的不認爲這是一個很好的主意從BroadcastReceiver中顯示AlertDialog。您應該創建一個Notification,並在用戶單擊它時打開一些活動。

+0

請您詳細說明 –

+0

您可以在這裏找到更多信息http://stackoverflow.com/questions/8350385/launching-dialog - 從服務 –

相關問題