了解 Android 上下文:(空对象引用)

Understanding Android Context: (null object reference)

我想构建一个简单的静态方法来获取当前 phone 的下一个预定警报。非静态实现,在 Main_Activity 中一切正常,但现在在单独的 class 作为静态方法中我得到错误:“android.content.Context.getContentResolver()' 在空对象引用上".

我想我理解得不够好Context。 我发现了这个:Static way to get 'Context' on Android? 但我认为这不是正确的方法,我想我只是遗漏了一些东西,有人可以帮忙吗?

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);
    }


}

(我不知道这是否重要,但 atm 控制器 Class 未作为 Activity 包含在清单文件中。我刚刚创建了一个新的 class 并从应用程序兼容性Activity)

这就是问题:new Controller();。永远不要自己实例化 Activity class(或从中派生的 class)。只有系统应该这样做,从而初始化所有必填字段。

上面评论中提到的 CommonsWare 在这方面似乎是正确的,

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

这是我将其更改为:

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;
    }

}

然后在某些 Activity.

中通过 Controller.nextAlarm(this)) 简单地调用它