1

I want to build a simple static method that gets me the next scheduled alarm from the current phone. Implemented non static, in the Main_Activity it all works as intended, but now in a seperate class as static method I get the error : "android.content.Context.getContentResolver()' on a null object reference".

I guess I am not understanding Context good enough. I found this: Static way to get 'Context' on Android? but I dont think it the right way to do it here, I think I am just missing something, can someone help ?

import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;


public class Controller extends AppCompatActivity {

    private static Controller staticController = new Controller();

    /**
     * Finds out what the next user scheduled alarm is.
     *
     * @return (String) next time the user has scheduled an alarm on his device.
     */
    protected static String nextAlarm() {

        String nextAlarmTime = null;

        // deprecated method will also detect non native alarm clocks!
        nextAlarmTime = Settings.System.getString(staticController.getContentResolver(),
                Settings.System.NEXT_ALARM_FORMATTED);

        // fallback if deprecated method does not find valid alarm time!
//        if (nextAlarmTime == null) {
//            AlarmManager am = (AlarmManager) staticController.getSystemService(Context.ALARM_SERVICE);
//            AlarmManager.AlarmClockInfo alarmInfo = am.getNextAlarmClock();
//            Long alarm_next = alarmInfo.getTriggerTime();
//            nextAlarmTime = (new Date(alarm_next)).toString();
//        }

        return nextAlarmTime;
    }

    // Do I need onCreate here ?
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


}

(I do not know if this is important but atm the Controller Class is not included in the Manifest file as Activity. I just created a new class and extended from AppCompatActivity)

Community
  • 1
  • 1
Martin Jäkel
  • 322
  • 6
  • 21

2 Answers2

1

What CommonsWare mentioned in the comments above seems to be right in this context,

Why are you not passing a Context (or ContentResolver) as a parameter to nextAlarm()?

Here is what I changed it to:

import android.app.AlarmManager;
import android.content.Context;
import android.provider.Settings;
import java.util.Date;

public class Controller extends {  **//does not need to be a Activity any more**

    /**
     * Finds out what the next user scheduled alarm is.
     *
     * @return (String) next time the user has scheduled an alarm on his device.
     */
    protected static String nextAlarm(Context context) { //**pass Context from other Activity** 

        String nextAlarmTime = null;

        // deprecated method will also detect non native alarm clocks!
        nextAlarmTime = Settings.System.getString(context.getContentResolver(),  //**reference parameter here**
                Settings.System.NEXT_ALARM_FORMATTED);

        // fallback if deprecated method does not find valid alarm time!
        if (nextAlarmTime == null) {
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // **reference parameter again**
            AlarmManager.AlarmClockInfo alarmInfo = am.getNextAlarmClock();
            Long alarm_next = alarmInfo.getTriggerTime();
            nextAlarmTime = (new Date(alarm_next)).toString();
        }

        return nextAlarmTime;
    }

}

Then just simply called it via Controller.nextAlarm(this)) in some Activity.

Martin Jäkel
  • 322
  • 6
  • 21
  • also this: https://tir38.com/archives/1423?utm_source=Android+Weekly&utm_campaign=5d9d6b9604-Android_Weekly_203&utm_medium=email&utm_term=0_4eb677ad19-5d9d6b9604-338020857 – Martin Jäkel May 02 '16 at 08:46
0

This is the problem: new Controller();. Never instanciate an Activity class (or a class derived from it) by yourself. Only the system should do that and thereby initialize all the required fields.

Henry
  • 42,982
  • 7
  • 68
  • 84