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
.
This method is :
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;
}
And the Exception:
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)
UPDATE:
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
is an Activity class in my android app, but the JSONParsing
class is a normal java class which calls this method from the HomeView
.