i' m getting nullPoint exception on getResources()
method. I'm starting the activity below from an another class with this:
Intent intent = new Intent(getActivity(), FooActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and from the activity
called, i have set the context that is used by a not-Activity class.
class FooActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[...]
Display display = new Display(...);
display.setContext(this.getApplicationContext());
}
}
class that use the getResources()
method:
class Display{
private static Context context;
public Display(...){ }
public void doSomething(){
**** NullPoint exception Row ****
Bitmap bitmpa = BitmapFactory.decodeResource(context.getResources(), R.drawable.custom_marker);
}
public void setContext(Context context) {
if (context == null)
this.context = context;
}
}
my Logcat:
08-24 20:18:46.656: E/AndroidRuntime(10526): FATAL EXCEPTION: main
08-24 20:18:46.656: E/AndroidRuntime(10526): java.lang.NullPointerException
08-24 20:18:46.656: E/AndroidRuntime(10526): at com.graphic.core.Display.doSomething(Display.java:43)
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-24 20:18:46.656: E/AndroidRuntime(10526): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 20:18:46.656: E/AndroidRuntime(10526): at android.os.Looper.loop(Looper.java:137)
08-24 20:18:46.656: E/AndroidRuntime(10526): at android.app.ActivityThread.main(ActivityThread.java:4895)
08-24 20:18:46.656: E/AndroidRuntime(10526): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 20:18:46.656: E/AndroidRuntime(10526): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 20:18:46.656: E/AndroidRuntime(10526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-24 20:18:46.656: E/AndroidRuntime(10526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-24 20:18:46.656: E/AndroidRuntime(10526): at dalvik.system.NativeStart.main(Native Method)
is the context not set properly , that cause the nullPoint?
Add the initialisation of the context to the Constructor like this :
public Display(Context context){
this.context = context;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.