Android 通知创建期间出现空指针异常
问题是通知中的Android 通知创建期间出现空指针异常,android,service,notifications,broadcastreceiver,Android,Service,Notifications,Broadcastreceiver,问题是通知中的NullPointerException。Broadcosting接收器类呼叫入Sms\u服务: public static void setMessage(long paramLong) { myContext.stopService(new Intent(myContext, Sms_Service.class)); intent = new Intent(myContext, Sms_Service.class); myContext.
NullPointerException
。Broadcosting接收器类呼叫入Sms\u服务
:
public static void setMessage(long paramLong)
{
myContext.stopService(new Intent(myContext, Sms_Service.class));
intent = new Intent(myContext, Sms_Service.class);
myContext.startService(intent);
((AlarmManager)myContext.getSystemService("alarm")).set(0, paramLongPendingIntent.getService(myContext, 0, intent, 0));
}
Sms\u服务
class:
public class Sms_Service extends Service
{
public static long localObject;
private MyCounter Timer;
private FakeYouSharedPreference myBalanceData;
private DataBaseAdapter myDB;
private MessageBean mySmsData;
private Vibrator phoneVibrate;
private MediaPlayer ringtonePlay;
private String uri;
public void createNotification()
{
int id= R.drawable.ic_sms;
//SmsManager sm = SmsManager.getDefault();
NotificationManager localNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 1;
android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
System.currentTimeMillis());
Cursor localCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "_id DESC");
Uri localUri = null;
if (localCursor.moveToFirst())
{
String str = localCursor.getString(localCursor.getColumnIndex("thread_id"));
Log.d("fakeyou", str);
localUri = Uri.parse("content://mms-sms/conversations/" + str);
}
Intent localIntent = new Intent("android.intent.action.VIEW", localUri);
localIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, localIntent, 0);
notification.setLatestEventInfo(this, this.mySmsData.getNumber(), this.mySmsData.getBody(), localPendingIntent);
notification.flags = (0x10 | notification.flags);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
localNotificationManager.notify(notifyID, notification);
}
@Override
public IBinder onBind(Intent paramIntent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
this.createNotification();
}
private class MyCounter extends CountDownTimer
{
public MyCounter(long arg2,long arg4)
{
super(localObject, arg2);
}
public void onFinish()
{
if (Sms_Service.this.ringtonePlay != null)
Sms_Service.this.ringtonePlay.stop();
Sms_Service.this.stopSelf();
}
public void onTick(long paramLong)
{
}
}
}
错误是:
09-10 08:53:46.862: E/AndroidRuntime(2653): FATAL EXCEPTION: main
09-10 08:53:46.862: E/AndroidRuntime(2653): java.lang.RuntimeException: Unable to create service com.fakeyou.Sms_Service: java.lang.NullPointerException
java.lang.NullPointerException
09-10 08:53:46.862: E/AndroidRuntime(2653): at Sms_Service.createNotification(Sms_Service.java:42)
09-10 08:53:46.862: E/AndroidRuntime(2653): at com.fakeyou.Sms_Service.onCreate(Sms_Service.java:70)
09-10 08:53:46.862: E/AndroidRuntime(2653): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2529)
09-10 08:53:46.862: E/AndroidRuntime(2653): ... 10 more
在
Sms\u service.java
的第42行:
android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
System.currentTimeMillis());
mysmdata
为空。您必须初始化此变量。请显示初始化mysmdata的代码部分。
如果你真的初始化了mysmdata,也许你搞错了
localCursor.moveToFirst()
如果查询ContentProvider时出错,也会引发NullPointerException:
Cursor localCursor=getContentResolver().query(Uri.parse()content://sms/inbox“”,null,null,null,“_iddesc”)代码>请在Sms_service.java
中标记第42行。mySmsData为空,因为您从未对其进行初始化。第42行的重复项是此android.app.Notification Notification=new android.app.Notification(id,THIS.mysmdata.getNumber()+:“+THIS.mysmdata.getBody(),System.currentTimeMillis());已准备好初始化我的代码mysmdata(private MessageBean mysmdata)MessageBean.class(get,set)public String getNumber(){返回this.number;}public String getBody(){返回this.body;}这些只是MessageBean对象的方法。。。但是你没有实例化这个对象!在“onCreate”中,您必须执行如下操作:this.mysmdata=newmessagebean()
并可能调用this.mysmdata.setNumber(“1”)
或其他什么。