简体   繁体   English

getResources()上的nullPoint异常

[英]nullPoint Exception on getResources()

i' m getting nullPoint exception on getResources() method. 我在getResources()方法上得到nullPoint异常。 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. 从所调用的activity ,我设置了一个非活动类使用的上下文。

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: 使用getResources()方法的类:

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: 我的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? 上下文设置不正确,会导致nullPoint吗?

Add the initialisation of the context to the Constructor like this : 像这样将上下文的初始化添加到构造函数中:

public Display(Context context){ 
       this.context = context;
   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM