2013-03-10 159 views
-5

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

Source Code

+0

https://docs.google.com/document/d/1yiU8UoHU3bXXWaIIk1LwwGP-EMiQ9l1gPY3qQz-djhQ/edit?usp=sharing – 2013-03-10 08:26:26

+0

发表您的logcat的痕迹 – DjHacktorReborn 2013-03-10 08:27:44

+2

,并在你的问题发布您的代码内嵌的_relevant_部分。 – Mat 2013-03-10 08:28:37

回答

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); 
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是什么?

相关问题