为什么我得到getApplicationcontext()null?

kAm*_*mol 4 android push-notification android-notifications android-intentservice

我不确定它有什么不对!我在这里读到Intentservice本身就是它的子类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
}
Run Code Online (Sandbox Code Playgroud)

但是我的价值是空的mainContext.任何建议都是最受欢迎的.

Hen*_*nry 6

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

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