2013-09-30 58 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也是如此。