我总是收到此错误,我是广播接收者的新手,对通知的经验很少,但是每次我调用Asynctask并在postExecute上调用通知时,都会得到此错误。我整天都在努力寻找解决方案,但似乎找不到解决方案。
Ps::使用通知在不使用broadcastReceiver的情况下也可以正常工作,但是我想使用它,因此即使应用程序是封闭的应用程序也可以保持用户更新。
public class CSVStringToListConverter implements Converter<String, List<Integer>> {
@Autowired
private Errors errors;
@Override
public List<Integer> convert(String arg0) {
List<Integer> list = new ArrayList<Integer>();
for (String s : arg0.split(",")) {
try {
list.add(Integer.parseInt(s));
} catch (NumberFormatException e) {
list = Collections.emptyList();
errors.rejectValue("fieldName", "fieldName.invalid", "Invalid integer in list");
}
}
return list;
}}
在这里我的MainActivity
11-03 16:09:06.482 29073-29073/com.example.jade.messaging E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jade.messaging, PID: 29073
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:102)
at android.app.PendingIntent.getActivity(PendingIntent.java:306)
at android.app.PendingIntent.getActivity(PendingIntent.java:272)
at com.example.jade.messaging.MainActivity.sendOnChannel1(MainActivity.java:77)
at com.example.jade.messaging.MainActivity$getTemp.onPostExecute(MainActivity.java:306)
at com.example.jade.messaging.MainActivity$getTemp.onPostExecute(MainActivity.java:240)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我的广播接收器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
createNotificationChannels();
notificationManager = NotificationManagerCompat.from(this);
stopAlarm();
}
private void createNotificationChannels() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(channel1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendOnChannel1() {
String title = "Notif";
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
//this is where I get the null pointer and if I remove this I get the error at the .build
pendingIntent1 = PendingIntent.getActivity(this, 1, intent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent1)
.setVibrate(new long[0])
.setAutoCancel(true)
.build();
notificationManager.notify(1, notification);
}
然后我在其中调用的方法将在主活动上运行MainActivity activity = new MainActivity();
@Override
public void onReceive(Context arg0, Intent arg1) {
// For our recurring task, we'll just display a message
activity.getValue();
}
,在执行后将运行notif。
AsyncTask
代码:
AsyncTask
我在asyctask上使用了弱引用,这就是为什么有活动的原因。在每个代码上。仅当我在asynctask上调用notif时,并且如果我在broadcastreceiver类上调用notif方法时,我的应用程序才会崩溃,我会收到有关存储数据的全局类的错误。说它为空。
请帮助