![](/img/trans.png)
[英]app crash with android.content.Context.getPackageName()' on a null object reference
[英]Understanding Android Context: (null object reference)
我想构建一个简单的静态方法,让我从当前手机获取下一个预定的闹钟。 实现非静态,在Main_Activity
它都按预期工作,但现在在一个单独的类中作为静态方法我得到错误:“ android.content.Context.getContentResolver()' on a null object reference ”。
我想我对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 控制器类未作为活动包含在清单文件中。我刚刚创建了一个新类并从 AppCompatActivity 扩展)
在上面的评论中提到的 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;
}
}
然后只需在某些活动中通过Controller.nextAlarm(this))
调用它即可。
这就是问题所在: new Controller();
. 永远不要自己实例化Activity
类(或从它派生的类)。 只有系统应该这样做,从而初始化所有必需的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.