2013-03-11 68 views
1

我對android很新,我有一個GCM客戶端和服務器,它工作正常。當應用程序運行時,我從服務器獲取通知。通知管理器不顯示任何類型的通知

但是,當應用程序沒有運行,我沒有得到任何迴應。

這裏是我用於生成通知的代碼:這是通過所謂的

private void generateNotification(Context context, String message) { 
     int icon = 1; 
     long when = System.currentTimeMillis(); 

     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 
     String title = context.getString(R.string.app_name); 
     Intent notificationIntent = new Intent(context, DemoActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 
    } 

@Override 
    protected void onMessage(Context context, Intent intent) { 
     generateNotification(context, "Tock Tock"); 
    } 

,當我看到logcat的我得到這樣一個錯誤:

Unable to find resource: 1 
W/ImageView( 58): android.content.res.Resources$NotFoundException: Resource ID #0x1 
W/ImageView( 58):  at android.content.res.Resources.getValue(Resources.java:892) 
W/ImageView( 58):  at android.content.res.Resources.getDrawable(Resources.java:580) 
W/ImageView( 58):  at android.widget.ImageView.resolveUri(ImageView.java:489) 
W/ImageView( 58):  at android.widget.ImageView.onMeasure(ImageView.java:592) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:306) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:989) 
W/ImageView( 58):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
W/ImageView( 58):  at android.widget.ScrollView.onMeasure(ScrollView.java:286) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
W/ImageView( 58):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
W/ImageView( 58):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
W/ImageView( 58):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
W/ImageView( 58):  at android.view.View.measure(View.java:8171) 
W/ImageView( 58):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801) 
W/ImageView( 58):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 
W/ImageView( 58):  at android.os.Handler.dispatchMessage(Handler.java:99) 
W/ImageView( 58):  at android.os.Looper.loop(Looper.java:123) 
W/ImageView( 58):  at com.android.server.ServerThread.run(SystemServer.java:506) 

不知道我犯了什麼錯誤。我正在關注here的示例教程。

在此先感謝。

回答

1

問題出在int icon = 1;

你應該在那裏添加一個真實的資源ID,所以它會顯示在通知欄中。你得到Resources$NotFoundException,因爲找不到資源編號爲1

例如,如果你把所謂xyz.png圖像到res/drawable文件夾,圖像的ID將R.drawable.xyz可以從代碼調用,如:

int icon = R.drawable.xyz;