ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

尝试在空对象引用上调用虚方法’android.view.View android.view.View.getRootView()’

2019-10-03 12:25:48  阅读:386  来源: 互联网

标签:android nullpointerexception


我收到这个错误,

“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.

标签:android,nullpointerexception
来源: https://codeday.me/bug/20191003/1849399.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有