重新启动后的警报管理器在意图时显示错误 NullPointerException

Posted

技术标签:

【中文标题】重新启动后的警报管理器在意图时显示错误 NullPointerException【英文标题】:Alarmmanager after reboot show error NullPointerException at intent 【发布时间】:2018-10-29 06:58:42 【问题描述】:

我想在手机重启后重置闹钟管理器

这是重启后重置警报的代码。手机重启后,我在 AlarmReset 类中从 SQLi 查询数据,并通过方法 au.setAlarm() 将数据发送到警报管理器。

public class AlarmReset  extends BroadcastReceiver 

    SQLiteDatabase db;
    DBHelper dbHelper ;
    Cursor cursor;
    String type_number;
    String op_name;
    String sNotes;

    @Override
    public void onReceive(Context context, Intent intent) 

        AlarmUtils au = new AlarmUtils();
        dbHelper = new DBHelper(context);

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
            // Set the alarm here.


            db = dbHelper.getWritableDatabase();

            String[] queryColumns = new String[]DBHelper.COL_DATE,DBHelper.COL_RQCODE , DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_OPTION_NAME;
           cursor = db.query(DBHelper.TABLE_NAME, queryColumns,null,null,null,null,null);

                while (cursor.moveToNext()) 


                    au.setAlarm(cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");

                

            
         //   db.close();

    


这是类 AlarmUtils 方法 setAlarm() 用于警报 2 时间。

   public class AlarmUtils  extends AppCompatActivity 

        public void setAlarm(String alarmDate, int rq_Code, String sNotes) 

            String[] parts = alarmDate.split("-");

            String day = parts[0];
            String month = parts[1];
            String year = parts[2];
            Calendar next1 = Calendar.getInstance();
            next1.set(Calendar.YEAR, Integer.parseInt(year));//year...
            next1.set(Calendar.MONTH, Integer.parseInt(month)-1);  //first month is 0!!! January is zero!!!
            next1.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)-1);  //1-31

            next1.set(Calendar.HOUR_OF_DAY,19);  //HOUR
            next1.set(Calendar.MINUTE,0);       //MIN
            next1.set(Calendar.SECOND, 2);       //SEC

            Calendar next2 = Calendar.getInstance();
            next2.set(Calendar.YEAR, Integer.parseInt(year));//year...
            next2.set(Calendar.MONTH, Integer.parseInt(month)-1);  //first month is 0!!! January is zero!!!
            next2.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));  //1-31

            next2.set(Calendar.HOUR_OF_DAY,7);  //HOUR
            next2.set(Calendar.MINUTE, 0);       //MIN
            next2.set(Calendar.SECOND, 2);       //SEC



            Date newDate1 = next1.getTime();
            Date newDate2 = next2.getTime();

            Intent myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
            myIntent.putExtra("Title", "Title");
            myIntent.putExtra("Notes", sNotes);

            PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code, myIntent, 0);
            PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), rq_Code+1, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent1);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, next2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS,pendingIntent2);

        

    

重启手机并打开应用程序后显示如下错误。

10-29 10:03:23.507 2977-2977/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.max.driver_app, PID: 2977
java.lang.RuntimeException: Unable to start receiver com.example.max.driver_app.AlarmReset: java.lang.NullPointerException
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2438)
    at android.app.ActivityThread.access$1700(ActivityThread.java:143)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5059)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:818)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
    at com.example.max.driver_app.AlarmUtils.setAlarm(AlarmUtils.java:53)
    at com.example.max.driver_app.AlarmReset.onReceive(AlarmReset.java:64)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)

setAlarm(AlarmUtils.java:53) 是行

意图 myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);

(AlarmReset.java:64) 是行

au.setAlarm(cursor.getString(0), Integer.parseInt(cursor.getString(1)), "测试!");

如何解决?

【问题讨论】:

getApplicationContext() 返回 null? ***.com/questions/21994612/… @user572575 +Rab 请注意:不要扩展应用程序,但更喜欢context on demand 【参考方案1】:

您应该避免实例化活动类,如果活动没有正确实例化,applicationContext 将始终为空正确实例化。您可以尝试更改方法以支持 Context 作为参数:

public static void setAlarm(Context context String alarmDate, int rq_Code, String sNotes) 
    // ...
    Intent myIntent = new Intent(context, AlarmReceiver.class);
    // ...

然后将上下文传递给BroadcastReceiver.onReceive 中的实用程序方法:

AlarmUtil.setAlarm(context, cursor.getString(0), Integer.parseInt(cursor.getString(1)), "Test !");

【讨论】:

以上是关于重新启动后的警报管理器在意图时显示错误 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

如何在每次打开日期范围选择器对话框时显示警报?

如何仅在应用程序首次启动时显示警报显示?

安装软件时显示runtime错误怎么解决

单击链接时显示警报视图

应用程序在 Xcode 上连接设备时显示警报视图,否则不会

Android 警报管理器在重新启动设备或从后台杀死应用程序后取消