重新启动后的Alarmmanager在意图上显示错误NullPointerException

时间:2018-10-29 06:58:43

标签: java android android-intent alarmmanager

我想在手机重启后重设警报管理器

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

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)是第

  

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

(AlarmReset.java:64)是第

  

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

如何修复?

1 个答案:

答案 0 :(得分:1)

您应避免实例化活动类,如果活动未正确地实例化,则applicationContext始终为null。您可以尝试更改方法以支持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 !");