【问题标题】:Notification Android Object Error通知 Android 对象错误
【发布时间】:2014-11-21 17:25:27
【问题描述】:

所以我正在阅读 Android 站点上的 Android Notification api 并复制他们作为演示的代码 但我有一个错误

mNotificationManager.notify(mId, mBuilder.build());

Object 中的Notify() 不能应用到(int, android.app.Notification)

当我去 Object.java Notify()

public final native void notify();

/**
 * Causes all threads which are waiting on this object's monitor (by means
 * of calling one of the {@code wait()} methods) to be woken up. The threads
 * will not run immediately. The thread that called {@code notify()} has to
 * release the object's monitor first. Also, the threads still have to
 * compete against other threads that try to synchronize on the same object.
 *
 * <p>This method can only be invoked by a thread which owns this object's
 * monitor. A thread becomes owner of an object's monitor
 * <ul>
 * <li>by executing a synchronized method of that object;</li>
 * <li>by executing the body of a {@code synchronized} statement that
 * synchronizes on the object;</li>
 * <li>by executing a synchronized static method if the object is of type
 * {@code Class}.</li>
 * </ul>
 *
 * @throws IllegalMonitorStateException
 *             if the thread calling this method is not the owner of this
 *             object's monitor.
 * @see #notify
 * @see #wait()
 * @see #wait(long)
 * @see #wait(long,int)
 * @see java.lang.Thread
 */

这是来自 Android 通知 API 的代码。

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

   private void sendNotification() {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.notification)
                        .setContentTitle("Notification")
                        .setContentText("My First Notification Bar!");
// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, notificationV.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(notificationV.class);
// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.

        mNotificationManager.notify(mId, mBuilder.build());

    }

这是代码的图像它似乎在寻找 Int 通知和字符串 但是一旦我输入它们就会显示一个错误

http://i.stack.imgur.com/JZ1hy.jpg

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

    检查您如何定义mNotificationManager。看起来好像您已将其定义为 Object,而实际上它应该是 NotificationManager。 .notify(int, Notification) 方法来自NotificationManager。

    【讨论】:

    • 我对上述文本进行了编辑,并将其放入我的导入中。我不知道问题是否出在 import android.support.v4.app.NotificationCompat;
    • 我只是按原样运行应用程序,出现错误,现在通知似乎正在传递。奇怪..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多