我正在添加一个按钮,在点击它时将用户返回到主屏幕,但是每次单击按钮时应用程序都会关闭。
答案 0 :(得分: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);
答案 1 :(得分: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
是什么?