0

现在活动被破坏,但状态图标仍然出现在状态栏上。你能在应用程序关闭的同时帮我删除图标吗?我怀疑 onDestroy 部分的问题... LogCat 信息是我在
notificationManager.cancel(NOTIF_ID); 行有空指针异常;

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

}

日志猫

09-30 17:24:51.052: E/AndroidRuntime(5200): FATAL EXCEPTION: main
09-30 17:24:51.052: E/AndroidRuntime(5200): java.lang.RuntimeException: Unable to destroy activity {com.flex_bak.eggcook/com.example.eggcook.Jajko}: java.lang.NullPointerException
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2693)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2711)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread.access$2100(ActivityThread.java:121)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:976)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.os.Looper.loop(Looper.java:130)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread.main(ActivityThread.java:3701)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at java.lang.reflect.Method.invoke(Method.java:507)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at dalvik.system.NativeStart.main(Native Method)
09-30 17:24:51.052: E/AndroidRuntime(5200): Caused by: java.lang.NullPointerException
09-30 17:24:51.052: E/AndroidRuntime(5200):     at com.example.eggcook.Jajko.onDestroy(Jajko.java:260)
09-30 17:24:51.052: E/AndroidRuntime(5200):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2680)
09-30 17:24:51.052: E/AndroidRuntime(5200):     ... 11 more
4

2 回答 2

1

从您发布的代码来看,它看起来并没有notificationManager在任何地方初始化,这肯定会给NPE. 我什至没有看到它被声明,但我想那是因为它可能是在之前完成的onCreate(),这很好。

您可能的意思是nm,而不是notificationManage因为您确实初始化了它。如果你认为你做了那么请显示你认为你在哪里初始化它。

你也可以检查它是否null在输入if语句之前,以防它nullonDestroy()被调用之前。

于 2013-09-30T15:42:02.707 回答
0

更新

只需更改此行:

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

对此

nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

它应该工作:-)

我认为您的 notificationManager 从未启动过,或者在您传递 onDestroy 方法之前它已经被销毁。包括一个 nullcheck 以避免此异常:

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

}
于 2013-09-30T15:42:25.740 回答