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)

不知道我在哪里犯了错误。我正在关注这里的示例教程。

提前致谢。

4

1 回答 1

1

问题出在int icon = 1;.

您应该在那里添加一个真实的资源 id,这样它就会显示在通知栏中。你得到了Resources$NotFoundException因为没有找到 id 的资源1

例如,如果您将一个名为的图像xyz.png放入res/drawable文件夹中,则该图像的 id 将是R.drawable.xyz可以从以下代码中调用的:

int icon = R.drawable.xyz;
于 2013-03-11T12:36:50.243 回答