尝试在空对象引用上调用虚拟方法“android.view.View android.view.View.getRootView()" [英] Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a null object reference

查看:23
本文介绍了尝试在空对象引用上调用虚拟方法“android.view.View android.view.View.getRootView()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,

"尝试调用虚方法'android.view.Viewandroid.view.View.getRootView()' 在一个空对象引用上"

"Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a null object reference"

这是我的代码.

black.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View bView = findViewById(R.id.lin);
            View root = bView.getRootView();
            root.setBackgroundColor(Color.parseColor("#000000"));
        }
    });

我有两个视图,一个叫做overlay_view,另一个叫做activity_main.我正在尝试更改overlay_view 的颜色,但出现此错误.我将 setcontentView 设置为activity_main,如果我将其切换到overlay_view,它不会给我一个错误.但是我不想将 setContentView 切换到 overlay_view 那么有没有另一种方法可以做到这一点?谢谢

I have two views, one called overlay_view and the other activity_main. I am trying to change the color of overlay_view but I am getting this error. I have the setcontentView to activity_main, and if I switch it to overlay_view it does not give me a error. However I do not want to switch the setContentView to overlay_view so is there another way to do this? Thanks

我有一个添加到overlay_view 的服务.该服务是从 mainactivity 中调用的.这是在主要活动中调用它的地方:

I have a service which adds in overlay_view. The service is called from the mainactivity. Here is where it is called from in the main activity:

    public void sendMessage(){
    Intent intent = new Intent(this, DrawOverAppsService.class);

    startService(intent);
    Intent intent1 = new Intent(this, MainActivity.class);
}

这里是服务:

public class DrawOverAppsService extends Service {

public static final String TAG = "DrawOverAppsService";



View mOverlayView;

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

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

    Log.d(TAG, "onCreate");


    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                    WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            PixelFormat.TRANSLUCENT);

    // An alpha value to apply to this entire window.
    // An alpha of 1.0 means fully opaque and 0.0 means fully transparent
    params.alpha = 0.1F;

    // When FLAG_DIM_BEHIND is set, this is the amount of dimming to apply.
    // Range is from 1.0 for completely opaque to 0.0 for no dim.
    params.dimAmount = 0.9F;

    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);


    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    mOverlayView = inflater.inflate(R.layout.overlay_view, null);

    wm.addView(mOverlayView, params);





}

@Override
public void onDestroy() {
    super.onDestroy();

    Log.d(TAG, "onDestroy");

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        wm.removeView(mOverlayView);


}
}

这是overlay_view的xml:

This is the xml for overlay_view:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF4081"
    android:orientation="vertical"></LinearLayout>

推荐答案

您的问题是您在服务中创建了一个视图,并希望在主活动中引用它以更改其背景颜色.你的行 View bView = findViewById(R.id.lin) 给你一个空值,因为系统在 activity_mainR.id.lin代码>.R.id.lin 确实存在于 overlay_view 中,这就是为什么在 setContentView() 中使用的视图不会出现错误的原因.

Your issue is that you create a view in a service and want to reference it in the main activity to change its background color. Your line View bView = findViewById(R.id.lin) is giving you a null value because the system cannot find R.id.lin in activity_main. R.id.lin does exist in overlay_view and that is why you don't get the error with that view used in setContentView().

服务类变量 mOverlayView 引用了您想要的视图.我不知道通过窗口管理器添加的视图的 findViewById() 功能,因此您需要在活动和服务之间实现连接.更改背景的服务代码如下所示:

The service class variable mOverlayView has a reference to the view you want. I don't know of findViewById() functionality for views added via the window manager, so you will need to implement a connection between the activity and the service. The service code to change the background will look something like this:

View bView = mOverlayView.findViewById(R.id.lin);
if (bView != null) {}
    View root = bView.getRootView();
    root.setBackgroundColor(Color.parseColor("#000000"));
}

在活动的 onClick 处理程序中,您将通知服务更改背景.有几种方法可以处理这个问题,您可能已经有了一种向服务发送命令的方法.如果没有,请查看 startService

In the onClick handler of the activity, you will notify the service to change the background. There are several ways to handle this and you may already have a way to send commands to the service. If not, take a look at startService

对该方法的每次调用都会导致对目标服务的 onStartCommand(Intent, int, int) 方法的相应调用,其意图在此处给出.这提供了一种向服务提交作业的便捷方式,而无需绑定和调用其接口.

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

您也可以设置本地广播管理器或绑定服务.搜索 "android活动服务通信",以获得有关处理此问题的替代方法的结果.

You can also set up a local broadcast manager or bind the service. Search for "android activity service communication" for results on alternate ways to handle this.

在继续之前,您可能还想熟悉这个问题.

You may also want to familiarize yourself with this issue before proceeding.

这篇关于尝试在空对象引用上调用虚拟方法“android.view.View android.view.View.getRootView()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆