-5

我正在添加一个按钮,单击该按钮可将用户返回到主屏幕,但是每次单击该按钮时应用程序都会强制关闭。

源代码

4

2 回答 2

1

我猜你正在获得NPE。

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);  <<<<Error is here NPE

Intent intent = new Intent();
Intent intent=new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);  <<<There must be Compile time error because you did not declare homeIntent
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
于 2013-03-10T08:31:09.620 回答
0

对于我所看到的,我认为您的问题在这里:

public void onClick(View v) {
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("Ticker Title")
.setContentTitle("Content Title")
.setContentText("Notifcation Content")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);


Intent intent = new Intent();
Intent intent=new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
}

并且更准确地表达了这一行的意图:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

您正在尝试将此意图添加到PendingIntent实际创建它之前。

你用的是什么homeIntent

于 2013-03-10T08:33:51.653 回答