getPackageName()返回null [英] getPackageName() returns null

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

问题描述

我在我的Andr​​oid应用程序的方法,它返回的绘制ID 根据它的名字,这种方法效果这么好正常的情况下,但是从一个单独的线程运行时,它返回,其上运行的 UIThread 此线程时也是如此。

I have a method in my android app that returns the drawable id according to it's name, this method works so well in the normal cases, but when running it from a separate thread it returns Null, even when running this thread on the UIThread.

这个方法:

public int get_drawable(int status){
     int res;
     res = getResources().getIdentifier("something"+ Integer.toString(status) , "drawable", getPackageName()); // this is the line which throws the Exception
     return res;
}

和异常:

02-04 13:43:14.644: WARN/System.err(594): java.lang.NullPointerException
02-04 13:43:14.664: WARN/System.err(594):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-04 13:43:14.664: WARN/System.err(594):     at com.example.solaceap.HomeView.get_drawable(HomeView.java:525)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.alter_light(JSONParsing.java:689)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.parse_hmm_response(JSONParsing.java:297)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.check_hmm_type(JSONParsing.java:218)
02-04 13:43:14.684: WARN/System.err(594):     at com.example.solaceap.JSONParsing.parse_hmm_response(JSONParsing.java:170)
02-04 13:43:14.684: WARN/System.err(594):     at com.example.solaceap.Login.parse_response(Login.java:143)
02-04 13:43:14.704: WARN/System.err(594):     at com.example.solaceap.Login.response_received(Login.java:129)
02-04 13:43:14.704: WARN/System.err(594):     at com.example.solaceap.Login$2$1.run(Login.java:106)
02-04 13:43:14.714: WARN/System.err(594):     at android.os.Handler.handleCallback(Handler.java:605)
02-04 13:43:14.724: WARN/System.err(594):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 13:43:14.724: WARN/System.err(594):     at android.os.Looper.loop(Looper.java:137)
02-04 13:43:14.724: WARN/System.err(594):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-04 13:43:14.724: WARN/System.err(594):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 13:43:14.724: WARN/System.err(594):     at java.lang.reflect.Method.invoke(Method.java:511)
02-04 13:43:14.724: WARN/System.err(594):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-04 13:43:14.734: WARN/System.err(594):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-04 13:43:14.734: WARN/System.err(594):     at dalvik.system.NativeStart.main(Native Method)

更新:

做了一些尝试,我发现后,该 getPackageName()是抛出该异常,甚至当试图静态地提供我的资源包的名字它给了我同样的一个结果。

after doing some tries i found that the getPackageName() is the one which throws that Exception, even when tried to statically provide my resources package name it gives me the same result.

HomeView 是一个Activity类在我的Andr​​oid应用程序,但 JSONParsing 类是调用一个普通的Java类此方法的 HomeView

HomeView is an Activity class in my android app, but the JSONParsing class is a normal java class which calls this method from the HomeView.

推荐答案

堆栈跟踪说,异常发生在<一href=\"http://androidxref.com/4.4.2_r1/xref/frameworks/base/core/java/android/content/ContextWrapper.java#87\"相对=nofollow> ContextWrapper.getResources() 。它只能NPE如果基本上下文为null。

The stacktrace says the exception happens in ContextWrapper.getResources(). It can only NPE if the base context is null.

如果你想你的活动之前调用该方法的基本上下文可以为空的onCreate()。你不能在其生命周期使用的活动作为一个上下文直到的onCreate()

The base context can be null if you're trying to call the method before your activity onCreate(). You cannot use an activity as a Context until onCreate() in its lifecycle.

此外,你不能用所有设置code涉及例如实例活动建立环境资源将无法运行。

Also, you cannot instantiate activities with new as all the setup code involving e.g. setting up context resources will not be run.

对于一个更好的答案我们需要知道什么 HomeView ,以及如何你实际实例化。

For a better answer we'd need to know what HomeView is and how you're actually instantiating it.

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

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