微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试在空对象引用上调用虚方法’android.view.View android.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,那么还有另一种方法吗?谢谢

编辑:
我有一个添加了overlay_view的服务.该服务是从mainactivity调用的.以下是主要活动中调用它的位置:

    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:

<?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_main中找不到R.id.lin. R.id.lin确实存在于overlay_view中,这就是为什么你没有得到setContentView()中使用的那个视图的错误.

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

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

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

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 activity service communication”以获得处理此问题的替代方法的结果.

在继续之前,您可能还想熟悉this issue.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐