1

问题在于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
4

2 回答 2

2

在第 42 行Sms_service.java

android.app.Notification notification = new android.app.Notification(id, this.mySmsData.getNumber() + " : " + this.mySmsData.getBody(),
            System.currentTimeMillis());

mySmsData一片空白。您必须初始化此变量。

于 2013-09-10T09:27:45.690 回答
0

请显示您初始化 mySmsData 的代码部分。如果你真的初始化了 mySmsData,也许你走错了行。

localCursor.moveToFirst()如果查询 ContentProvider 出现问题,也会抛出 NullPointerException:

Cursor localCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "_id DESC");

于 2013-09-10T09:58:01.567 回答