, Я чувствую, что ваш ответ сработает, поэтому я приму его, спасибо!
учаю эту ошибку,
«Попытка вызвать виртуальный метод 'android.view.View android.view.View.getRootView ()' для пустой ссылки на объект»
Вот мой код.
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. Услуга вызывается из основной деятельности. Вот откуда он вызывается в основной деятельности:
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);
}
}
Это xml для 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>