0

Working on Android I am creating an SDK that creates a service to display an overlay on the client app. I want to be able to create ImageViews and other widgets using the context of my class which extends the Service class. This is cut down version of what I am doing.

public class MyService extends Service {

private WindowManager windowManager;
private ImageView myImage;


@Override
public void onCreate(){
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    return START_NOT_STICKY;
}

public  void startService (Context ctx) {
    context = ctx;
    Intent intent = new Intent(ctx, MyService.class);
    ctx.startService(intent);
   // have also tried new Intent(this, MyService.class);
   // startService(intent);

}

public void displayMyImage () {
    if (chatHead == null) {
        chatHead = new ImageView(this);// explodes here due to null 
    }
    chatHead.setImageResource(R.drawable.myimage);

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.BOTTOM | Gravity.LEFT;
    params.x = 10;
    params.y = 100;


    windowManager.addView(chatHead, params);


}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
}

I get a java.lang.NullPointerException on the line chatHead = new ImageView(this)

The reason I am trying this is from this article http://www.piwai.info/chatheads-basics/ where they are using this to create an image view within the service. How can I get it to use the context of the service and not need me to pass in the context of the client/host app?

Edit: just putting in full error from log

java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.View.<init>(View.java:3438)
at android.widget.ImageView.<init>(ImageView.java:114)
glogic
  • 4,042
  • 3
  • 28
  • 44
  • Did you try using `getApplicationContext()` instead of `this`? – Apoorv Sep 13 '14 at 07:44
  • just tried it. Same null pointer exception java.lang.NullPointerException at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109) – glogic Sep 13 '14 at 07:51
  • @glogic May be you called displayMyImage from the wrong place. I used your code and call displayMyImage from onCreate and ImageView successfully added to the screen. – Bracadabra Sep 13 '14 at 08:19
  • @Vang wow you are right. Any ideas why this is the case? I was calling displayMyImage from a different class on that service object after the service had started running. I made sure that the onCreate and the onStartCommand was being called before I called the displayMyImage. I can make it work this way but I am very curious as to why it is the case – glogic Sep 13 '14 at 12:25
  • @glogic It seems that when you call displayMyImage the service object isn't fully constracted, it's hard to say more without code. – Bracadabra Sep 14 '14 at 09:06

1 Answers1

0

The most easiest solution is create a Global Application context under the Application class of your project.

in that application class write the below code to intitialize your context and access it from anywhere.It will be initialized when your application starts.and it will solve your NPE

For Application class code:

public class MyApplication extends Application{

private static Context context;

public void onCreate(){
    super.onCreate();
    MyApplication.context = getApplicationContext();
}

public static Context getAppContext() {
    return MyApplication.context;
}

}

Now Modify your displayImage like this:

public void displayMyImage () {
if (chatHead == null) {
    chatHead = new ImageView(MyApplication.getAppContext());// NPE won't occur
......
}
}
Chiradeep
  • 973
  • 12
  • 32
  • Is it safe to have a class that extends application with in a library? Lets say someone drops in the aar file into their project and they also have a class that extends application. Wont this cause problems? (I am very unsure as I am stilling learning all of this as I go) – glogic Sep 13 '14 at 12:49
  • It is absolute safe and followed by various developers – Chiradeep Sep 13 '14 at 14:15