我想选择在通知栏中点击后打开的活动中删除我的通知。我使用以下代码:
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Log.i(TAG, "after NotificationManager");
Intent intent = new Intent(this,test.class);
intent.putExtra("title", title);
intent.putExtra("subject", subject);
intent.putExtra("notificationid", count);
Log.i(TAG, title);
Log.i(TAG, subject);
PendingIntent pi = PendingIntent.getActivity(this, count, intent,0);
Notification n = new Notification(R.drawable.icon,subject,System.currentTimeMillis());
n.setLatestEventInfo(this, title, subject, pi);
n.defaults = Notification.DEFAULT_LIGHTS;
nm.notify(count, n);
我的test.java看起来像这样:
public class test extends Activity {
TextView Title;
TextView Subject;
Button Clear;
protected int notificationid;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String title="";
String subject="";
Bundle extras = getIntent().getExtras();
if(extras !=null) {
title = extras.getString("title");
subject = extras.getString("subject");
notificationid = extras.getInt("notificationid");
}
Title = (TextView) findViewById(R.id.Title);
Subject = (TextView) findViewById(R.id.Subject);
Title.setText(title);
Subject.setText(subject);
Clear.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
NotificationManager nm ;
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Log.i(getClass().getSimpleName(), " onClick inside test caled....");
nm.cancel(notificationid);
}
});
}
}
但应用程序意外关闭。任何人都可以帮助我吗? 谢谢!
答案 0 :(得分:2)
你错过了
Clear=(Button) findViewById(R.id.ClearButtonId);
这在你的test
班级?
一定是问题所在。您可能会收到NullPointerException
,因为您尚未解析Button
组件并且您正在尝试直接使用它。
答案 1 :(得分:1)
与意外代码相关的错误是什么?你能详细说明导致崩溃的异常吗?
您使用LogCat吗?如果没有,请查看http://developer.android.com/guide/developing/tools/logcat.html。要直接在命令提示符下查看logcat日志,请打开命令提示符并转到找到android sdk的目录。运行adb logcat
。您应该开始看到logcat生成的错误,警告等的打印输出。
一旦你能够看到使用logcat的问题(它会告诉你NullPointerException,ResourceNotFoundException等以及导致问题的代码行),如果你无法从中找出崩溃的来源然后在此处粘贴错误信息。
答案 2 :(得分:1)
无法看到你的LogCat输出,很难分辨出错误是什么,但是从查看代码(如果你只粘贴部分代码当然可能是错误的),你就不会初始化在notificationid
的情况下,您的extras == null
变量。如果您正在使用Eclipse进行开发,请查看LogCat视图并找到错误堆栈跟踪。
看到Kyle的回答后编辑:他对LogCat的评价; - )
答案 3 :(得分:0)
尝试使用FLAG_AUTO_CANCEL:
n.flags |= Notification.FLAG_AUTO_CANCEL;
如果在用户点击通知时应取消通知,则应将位置按位进入应设置的标志字段。