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

时间:2016-04-27 11:31:48

标签: java android static alarmmanager android-context

我想构建一个简单的静态方法,让我从当前手机获取下一个预定的闹钟。实现非静态,在Main_Activity它都按预期工作,但现在在一个单独的类中作为静态方法我得到错误:“ android.content.Context.getContentResolver()'on a空对象引用 “。

我想我不太了解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);
    }


}

(我不知道这是否很重要,但是我的控件类不作为Activity包含在Manifest文件中。我只是创建了一个新类并从AppCompatActivity扩展而来)

2 个答案:

答案 0 :(得分:1)

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

  

为什么不将Context(或ContentResolver)作为参数传递   到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))调用它。

答案 1 :(得分:0)

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