2013-03-10 154 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是什麼?

相關問題