通知中的问题是NullPointerException
。在Sms_Service
中广泛接收接收器类:
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_Service
上课:
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
答案 0 :(得分:2)
在Sms_service.java
的第42行:
android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
System.currentTimeMillis());
mySmsData
为空。你必须初始化这个变量。
答案 1 :(得分:0)
请显示您初始化mySmsData的代码部分。 如果你真的初始化了mySmsData,也许你得错了。
如果查询ContentProvider出错, localCursor.moveToFirst()
也会抛出NullPointerException:
Cursor localCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "_id DESC");