Java 使用Android通知时出现运行时异常。

Java 使用Android通知时出现运行时异常。,java,android,notifications,Java,Android,Notifications,这是我的CreateNotification类。代码取自android页面 代码: package your.notification.manager; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; impo

这是我的CreateNotification类。代码取自android页面

代码:

package your.notification.manager;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.view.View;

public class CreateNotification extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this, NotificationReceiver.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

    }


} 
错误:

05-05 12:51:38.687: E/AndroidRuntime(8629): Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.crumbs.main id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)

你的代码运行良好

只需清理项目并再次运行