0

嗨任何人可以幫助我的錯誤: 不能使從類型ContextWrapper排隊的靜態引用非靜態方法getBaseContext()靜態參照非靜態方法getBaseContext()

PendingIntent pending_intent = PendingIntent.getActivity(getBaseContext(), 
       0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT); 

在代碼:

public static NotificationManager notificationManager; 
    public static final int NOTIF_ID = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_jajko); 

     text = (TextView) findViewById(R.id.tvTime); 
     play = (Button) findViewById(R.id.butStart); 
     miekko = (Button) findViewById(R.id.butMiekko); 
     srednio = (Button) findViewById(R.id.butSrednio); 
     twardo = (Button) findViewById(R.id.butTwardo); 

     miekko.setOnClickListener(this); 
     srednio.setOnClickListener(this); 
     twardo.setOnClickListener(this); 
     play.setOnClickListener(this); 

     mp = MediaPlayer.create(Jajko.this, R.raw.alarm); 

     showNotification(this); 

    } 

    public static void showNotification(Context context) { 
     final Intent result_intent = new Intent(context, Jajko.class); 

     result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     /*TaskStackBuilder stack_builder = TaskStackBuilder.create(context); 
     stack_builder.addParentStack(Jajko.class); 
     stack_builder.addNextIntent(result_intent); 

     PendingIntent pending_intent = stack_builder.getPendingIntent(0, 
       PendingIntent.FLAG_UPDATE_CURRENT);*/ 
     PendingIntent pending_intent = PendingIntent.getActivity(getBaseContext(), 
       0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(
       context); 

     Resources res = context.getResources(); 
     builder.setContentIntent(pending_intent) 
       .setSmallIcon(R.drawable.icon) 
       .setLargeIcon(
         BitmapFactory.decodeResource(res, R.drawable.icon)) 
       .setTicker("Egg Timer").setWhen(System.currentTimeMillis()) 
       .setAutoCancel(false).setContentTitle("Egg Timer") 
       .setContentInfo("").setContentText(""); 
     Notification n = builder.build(); 
     n.flags = Notification.FLAG_ONGOING_EVENT; 

     notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(NOTIF_ID, n); 
    } 

回答

0

你應該能夠使用:

PendingIntent pending_intent = PendingIntent.getActivity(context, 
      0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT); 

而不是

+0

是的,你是天才!這段代碼解決了我的問題!謝啦:) – bakusek

0

問題是showNotification是一個static方法,但您需要調用getBaseContext()來提供上下文。由於這不是static方法,所以不能從一個方法調用它。擺脫static修飾符showNotification