2013-06-18 48 views
0

我正在做一個非常簡單的例子,我想要一個按鈕來生成通知。我不是在尋找互動(所以沒有意圖);只是要發佈一個簡單的通知。 用戶界面只包含一個按鈕,單擊該按鈕即可生成通知。MainActivity在手機上失敗

下面的代碼:

package ank.notificationdemo; 

import android.R.drawable; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 
    Button btn_notify; 
    Notification noti; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btn_notify = (Button) findViewById(R.id.btn_notify); 
     noti = new Notification.Builder(MainActivity.this) 
       .setContentTitle("Alert!").setContentText("Shipwreck at 22N-56E. Hurry!").setSmallIcon(drawable.ic_dialog_alert) 
       .build(); 

     btn_notify.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       noti.flags |= Notification.FLAG_AUTO_CANCEL; 
       mgr.notify(100, noti); 
      } 
     }); 
    } 
} 

的問題是,這個工程在模擬器上,但主要活動失敗到我的Android手機(運行ICS)的加載。然而,其他例子工作得很好。

有什麼線索可能會出錯?

在此先感謝!

+4

你能提供logcat報告嗎? – Dimmerg

+0

對於收集logcat報告使用'adb logcat' – Arseniy

+0

@Dimmerg您是指Eclipse IDE中的logcat報告?它在我看來相當大。我應該在這裏粘貼一切還是上傳文本文件並提供鏈接? (對不起,如果問題是愚蠢的!) – dotslash

回答

1

想你的代碼出來,原來的問題是使用Notification.Builder.build()

看起來他們只是在果凍豆,並在最新的更新到支持庫改成了.build()

因此,只需導入新的支持庫並使用NotificationCompat.Builder代替。應該解決你的問題。

+0

但是這意味着我的應用程序在小於4.0的版本上打破了,不是嗎? – dotslash

+0

如果您使用支持庫,則不需要。如果你使用它,那麼方法.build()將會正常工作。 – SeanSWatkins

+0

不夠感謝你!謝謝你,謝謝你,謝謝你!!過去兩天我一直在爲此付出代價,最後我有一種方法可行! 但你是什麼意思?「看起來他們只是將它改爲.build()在果凍豆和最新更新到支持庫」? 這是否意味着整個Class結構被替換爲一個.build()?如果沒有,是不是.build()之前呢?請解釋並非常感謝你! – dotslash

0

我記得我的Nexus4有一個類似的問題。然而,它是通過在NotificationBuilder中設置一個Intent來解決的。

+0

不幸的是,添加一個意圖仍然崩潰。我怎樣才能在這裏粘貼修改後的代碼? – dotslash