简体   繁体   English

理解Android Context:(空对象引用)

[英]Understanding Android Context: (null object reference)

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 ".实现非静态,在Main_Activity它都按预期工作,但现在在一个单独的类中作为静态方法我得到错误:“ android.content.Context.getContentResolver()' on a null object reference ”。

I guess I am not understanding Context good enough.我想我对Context理解不够好。 I found this: Static way to get 'Context' on Android?我发现了这个: 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) (我不知道这是否重要,但 atm 控制器类未作为活动包含在清单文件中。我刚刚创建了一个新类并从 AppCompatActivity 扩展)

What CommonsWare mentioned in the comments above seems to be right in this context,在上面的评论中提到的 CommonsWare 在这种情况下似乎是正确的,

Why are you not passing a Context (or ContentResolver) as a parameter to nextAlarm()?为什么不将 Context(或 ContentResolver)作为参数传递给 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.然后只需在某些活动中通过Controller.nextAlarm(this))调用它即可。

This is the problem: new Controller();这就是问题所在: new Controller(); . . Never instanciate an Activity class (or a class derived from it) by yourself.永远不要自己实例化Activity类(或从它派生的类)。 Only the system should do that and thereby initialize all the required fields.只有系统应该这样做,从而初始化所有必需的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空对象引用上的android.content.Context.getPackageName()导致应用崩溃 - app crash with android.content.Context.getPackageName()' on a null object reference android.content.Context.getResources() 在空对象引用上 - android.content.Context.getResources() on a null object reference android.content.Context.getPackageName() 在 null object 参考 - android.content.Context.getPackageName() on a null object reference 在空对象引用上接收“ android.content.Context android.content.Context.getApplicationContext()” - Receiving 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 尝试在空对象引用上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()' - Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference Android:空对象参考 - Android: Null object reference Android Null对象参考 - Android Null object Reference 空对象引用上的上下文“getApplicationContext()”问题 - problem with Context "getApplicationContext()' on a null object reference" 尝试在 Android Studio 中的 null object 参考上调用虚拟方法 'android.content.Context.getApplicationInfo()' - Attempt to invoke virtual method 'android.content.Context.getApplicationInfo()' on a null object reference in Android Studio Android片段-空对象参考 - Android fragment - null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM