gpt4 book ai didi

android - 创建通知时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 21:34:29 25 4
gpt4 key购买 nike

问题是通知中的NullPointerExceptionSms_Service 中的 Broadcosting 接收类调用:

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

最佳答案

Sms_service.java 的第 42 行:

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

mySmsData 为空。您必须初始化此变量。

关于android - 创建通知时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18714787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com