错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tongzhi

*NotificationManager的主要方法有

(1)notify(int id,Notification notification);

         notify(String tag,int id,Notification notification);

这两个方法主要用于在通知栏给出提示。

其中id是唯一标识;参数notification是一个通知对象Notification类,不能为null;参数tag是该通知字符串标示,可以为null

(2)cancel(int id);

         cancel(String tag,int id);

         cancekAll();

这3个方法用于取消显示的通知,其中id是通知的唯一标识;参数tag是通知的字符串标识;最后一种方法用于取消所有先前显示的通知

*Notification:通过构造该类,实现通知的具体内容

它常用的构造方法有:

    Notification();

    Notification(int icon,CharSequence tickerText,long when);//图标;显示的通知的文本;产生时间

例如:

 Notification notification=new Notification(R.drawable.ic_launcher,text,Sytem.currentTimeMillis());

 

1、获得通知栏管理器NotificationManager

 NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

nm.notify(int id,Notification notification)//通知的唯一标识

2、通知Notification

PendingIntent contentIntent=PendingIntent.getActivity(this,0,new Intent(this,Activity2.class),0);

noticafication.setLatestEventInfo(context, contentTitle, contentText, contentIntent);//上下文;通知栏标题文字;内容文字;点击后的动作

notification.default I=Notification.DEFAULT_SOUND;//添加声音

notification.default I=Notification.DEFAULT_VIBRATE;添加震动

【我的实例:】

错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tongzhi_第1张图片

【代码:】

package com.example.tongzhi;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
	private NotificationManager nm;

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

	@SuppressWarnings("deprecation")
	public void showNotification() {
		CharSequence text = "wwy->我是内容!";
		Notification notification = new Notification(R.drawable.ic_launcher,
				text, System.currentTimeMillis());
		PendingIntent pendIntent = PendingIntent.getActivity(MainActivity.this,
				0, new Intent(), 0);
		notification.setLatestEventInfo(MainActivity.this, "我是标题:", text,
				pendIntent);
		notification.defaults |=Notification.DEFAULT_SOUND;
		notification.defaults |=Notification.DEFAULT_VIBRATE;
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nm.notify(R.string.hello_world, notification);

	}

}


 

【调试错误:】

12-10 21:32:17.980: E/AndroidRuntime(21966): FATAL EXCEPTION: main
12-10 21:32:17.980: E/AndroidRuntime(21966): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tongzhi/com.example.tongzhi.MainActivity}: java.lang.IllegalArgumentException: contentView required: pkg=com.example.tongzhi id=2131034114 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 kind=[null])
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.os.Looper.loop(Looper.java:153)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread.main(ActivityThread.java:5299)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at java.lang.reflect.Method.invoke(Method.java:511)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at dalvik.system.NativeStart.main(Native Method)
12-10 21:32:17.980: E/AndroidRuntime(21966): Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.example.tongzhi id=2131034114 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 kind=[null])
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.os.Parcel.readException(Parcel.java:1429)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.os.Parcel.readException(Parcel.java:1379)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:228)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.NotificationManager.notify(NotificationManager.java:132)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.NotificationManager.notify(NotificationManager.java:108)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at com.example.tongzhi.MainActivity.showNotification(MainActivity.java:23)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at com.example.tongzhi.MainActivity.onCreate(MainActivity.java:15)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.Activity.performCreate(Activity.java:5122)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-10 21:32:17.980: E/AndroidRuntime(21966):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-10 21:32:17.980: E/AndroidRuntime(21966):  ... 11 more

【错误原因:】

没有这一句:
notification.setLatestEventInfo(MainActivity.this, "我是标题:", text,pendIntent);

即为通知设置点击事件。

你可能感兴趣的:(异常类)