为什么我得到 getApplicationcontext() null? [英] Why am I getting getApplicationcontext() null?

查看:114
本文介绍了为什么我得到 getApplicationcontext() null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定它有什么问题!我在这里读到 Intentservice 本身就是 Context 的子类.

I'm not sure whats wrong in it! I read here that Intentservice is itself a subclass of Context.

public class GCMNotificationIntentService extends IntentService {
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context mainContext;
    WriteFile writeFile;

    public GCMNotificationIntentService() {
        super("GcmIntentService");
        mainContext = getApplicationContext();
        writeFile = new WriteFile(mainContext);
    }
    // My rest of the code
}

但是我得到了 mainContext 的空值.欢迎提出任何建议.

but I'm getting null value for mainContext. Any suggestions are most welcome.

推荐答案

在构造函数中访问应用上下文还为时过早.尝试将此代码移动到 onCreate 方法中.

In the constructor it is way too early to access the app context. Try to move this code into the onCreate method.

有关生命周期的更多数据可以在文档中找到一个>

More datails about the life cycle can be found in the documentation

这篇关于为什么我得到 getApplicationcontext() null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆