2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > android 虚方法 尝试在空对象引用上调用虚方法’android.view.View android.view.View.getRootView()’...

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

时间:2023-08-14 22:30:48

相关推荐

android 虚方法 尝试在空对象引用上调用虚方法’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:

android:id="@+id/lin"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FF4081"

android:orientation="vertical">

解决方法:

您的问题是您在服务中创建视图并希望在主活动中引用它以更改其背景颜色.你的行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.

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

标签:android,nullpointerexception

来源: https://codeday.me/bug/1003/1849399.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。