Java Onclick按钮使应用程序崩溃
我添加了一个按钮,当用户单击该按钮时,该按钮将返回主屏幕,但是每次单击该按钮时,应用程序都会强制关闭Java Onclick按钮使应用程序崩溃,java,android,eclipse,Java,Android,Eclipse,我添加了一个按钮,当用户单击该按钮时,该按钮将返回主屏幕,但是每次单击该按钮时,应用程序都会强制关闭 我想你正在获得NPE PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); <<<<Error is here NPE Intent intent = new Intent(); Intent intent=new Intent(Intent.ACTION_MAIN); homeI
我想你正在获得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);
pendingent-pIntent=pendingent.getActivity(this,0,intent,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);
在实际创建之前,您正试图将此意图添加到pendingent
您正在使用的homeIntent
是什么?发布日志跟踪,并在问题中内联发布代码的相关部分。