2013-09-30 132 views
0

破壞應用程序解決!感謝幫助! 現在活動已破壞,但狀態欄上仍顯示狀態圖標。當應用程序關閉時,您可以在刪除圖標的同時幫助我嗎?我懷疑onDestroy節的問題...android無法破壞我的活動


我有一個簡單的應用程序狀態欄圖標。當Activity開始並且用戶按下onPause()時,狀態欄圖標apper。點擊這個圖標恢復應用程序,但是當我想要銷燬活動時,我有一個錯誤。有人可以幫助這個?

private 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); 

     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("test") 
      .setWhen(System.currentTimeMillis()) 
      .setAutoCancel(false) 
      .setContentTitle("title") 
      .setContentInfo("cinfo") 
      .setContentText("ctext"); 
     Notification n = builder.build(); 
     n.flags = Notification.FLAG_ONGOING_EVENT; 

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

public void onDestroy() { 
    try { 
     mp.release(); 
     if (isFinishing()) { 
      notificationManager.cancel(NOTIF_ID); 
     } 
    } catch (Exception e) { 
    } 
    super.onDestroy(); 

} 
+0

可以請發佈您的代碼?至少Jajko.java:252線?這條線有些東西是空的。 – fasteque

+0

這行代碼是什麼Jajko.java:252 –

回答

0

在這種情況下,必須調用super.onDestroy來銷燬活動,因爲它是android生命週期的一部分。

所以你可以把你的代碼放在try/catch中,它會起作用。

public void onDestroy() { 
     try { 
      mp.release(); 
      if (isFinishing()) { 
       notificationManager.cancel(NOTIF_ID); 
      } 
     } catch (Exception e) { 
     } 
     super.onDestroy(); 
    } 
+0

現在活動已經破壞,但狀態欄上仍然顯示狀態圖標。當應用程序關閉時,您可以在刪除圖標的同時幫助我嗎?我懷疑onDestroy節的問題... – bakusek

+0

請upvote答案,如果你覺得它有用? –

0

你的方法有NullPointerException

public void onDestroy() { 
    mp.release(); 
if (isFinishing()) { 
    notificationManager.cancel(NOTIF_ID); 
} 
super.onDestroy(); 

} 

調用mp.release();驗證其不null

我會寫它像以前一樣:

public void onDestroy() { 

    if(mp != null){ 
     mp.release(); 
    } 

if (isFinishing()) { 
    notificationManager.cancel(NOTIF_ID); 
} 
super.onDestroy(); 

} 

但是相同的驗證你可以穿上notificationManager也是如此。