安卓:this.getApplication()返回NULL指针 [英] Android: this.getApplication() returns NULL pointer

查看:256
本文介绍了安卓:this.getApplication()返回NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个活动,这是相同的应用程序中以下行,但在不同的包:

I'm running the following line in an Activity, which is within the same application, but in a different package:

AppObject appObj = (AppObject)this.getApplication();
// FYI: AppObject is my extension class of Application.

而只返回一个空指针,而当我将它移动到主包,并从那里运行它返回预期的应用提供参考。

It returns only a null pointer, while when I move it to the "main" package and run it from there it returns the application reference as expected.

我定义的活动我AndroidManifest.xml中的完全限定类名,因为它是在另一个包:<活动机器人:名称=com.foo.bar.TestActivity >< /活动>

I've defined the activity in my AndroidManifest.xml with the full qualified class name, since it is in another package: <activity android:name="com.foo.bar.TestActivity"></activity>

什么我已经做不同的任何想法?

Any ideas of what I've to do different?

感谢


更新:作为建议如下 Android的一个问题:NAME =AppObject已经在&LT;用途&gt; 在AndroidManifest.xml中的标签

Update: As suggested in a question below android:name="AppObject" was already in the <application> tag of the AndroidManifest.xml

推荐答案

打电话重要的是 getApllication()在活动的的onCreate()方法,而不是在构造函数中。

It's important to call getApllication() in the activity's onCreate() method, not in the constructor.

这篇关于安卓:this.getApplication()返回NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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