简体   繁体   English

如何使用服务的上下文创建ImageView

[英]How to use the context of a Service to create ImageView

Working on Android I am creating an SDK that creates a service to display an overlay on the client app. 在Android上工作,我正在创建一个SDK,该SDK创建了一项服务以在客户端应用程序上显示覆盖。 I want to be able to create ImageViews and other widgets using the context of my class which extends the Service class. 我希望能够使用扩展Service类的类的上下文来创建ImageViews和其他小部件。 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) 我在chatHead = new ImageView(this)行上得到java.lang.NullPointerException

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. 我尝试此操作的原因来自这篇文章http://www.piwai.info/chatheads-basics/ ,他们在此使用它在服务中创建图像视图。 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)

The most easiest solution is create a Global Application context under the Application class of your project. 最简单的解决方案是在项目的Application类下创建Global Application上下文。

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 在该应用程序类中,编写以下代码以初始化您的上下文并可以从任何位置访问它。它将在您的应用程序启动时初始化,它将解决您的NPE

For Application class code: 对于应用程序类代码:

public class MyApplication extends Application{ 公共类MyApplication扩展了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: 现在像这样修改displayImage:

public void displayMyImage () {
if (chatHead == null) {
    chatHead = new ImageView(MyApplication.getAppContext());// NPE won't occur
......
}
}

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

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