所以我有一个夜间模式过滤器应用程序,它对主要活动有透明活动,并且在屏幕中间有一个按钮,应该将透明活动更改为黑色“屏幕过滤器”,用于夜间模式。我遇到的问题是 如果有人回答,那就很好 W / System.err:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.view.View.setBackgroundColor(int)' W / System.err:at com.example.kenert.allinoneapp.Nightmode.turnNightOn(Nightmode.java:91) W / System.err:at com.example.kenert.allinoneapp.Nightmode.nightmodeButtonClicked(Nightmode.java:78) W / System.err:at java.lang.reflect.Method.invoke(Native Method) W / System.err:at java.lang.reflect.Method.invoke(Method.java:372)
主要活动代码:
import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.SeekBar;
public class Nightmode extends AppCompatActivity {
private boolean nightmodeOnOff;
public ImageButton modeOnOffButton;
private int brightness;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this,NightmodeFilter.class));
setContentView(R.layout.activity_nightmode);
modeOnOffButton = (ImageButton) findViewById(R.id.nightmodeOnOffButton);
nightmodeOnOff = false;
int prog;
//Seekbar
SeekBar skbar = (SeekBar) findViewById(R.id.nightModeBar);
skbar.setMax(255);
skbar.setKeyProgressIncrement(127);
try {
brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
skbar.setProgress(brightness);
skbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
if (progress <= 25) {
brightness = 25;
} else {
brightness = progress;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
WindowManager.LayoutParams lpp = getWindow().getAttributes();
lpp.screenBrightness = brightness/(float)255;
getWindow().setAttributes(lpp);
}
});
}
public void nightmodeButtonClicked(View view) {
try {
if (nightmodeOnOff) {
nightmodeOnOff = false;
turnNightOff();
} else {
nightmodeOnOff = true;
turnNightOn();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void turnNightOn() {
try {
modeOnOffButton.setImageResource(R.drawable.nightmodeonbutton);
findViewById(R.id.activity_nightmode_filter).setBackgroundColor(Color.parseColor("#99000000"));
} catch (Exception e) {
e.printStackTrace();
}
}
private void turnNightOff() {
try {
modeOnOffButton.setImageResource(R.drawable.nightmodeonoffbutton);
findViewById(R.id.activity_nightmode).setBackgroundColor(0x66000000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
turnNightOff();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
turnNightOff();
}
@Override
protected void onDestroy() {
super.onDestroy();
turnNightOff();
}
@Override
protected void onPause() {
super.onPause();
turnNightOff();
}
}
透明活动代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
public class NightmodeFilter extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
setContentView(R.layout.activity_nightmode_filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
}
透明活动xml和主题:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:theme="@style/Theme.Transparent"
android:id="@+id/activity_nightmode_filter"
android:layout_height="wrap_content"
android:background="@color/Transparent"
tools:context="com.example.kenert.allinoneapp.NightmodeFilter"
android:layout_width="match_parent"
android:orientation="horizontal">
</LinearLayout>
主题:
<style name="Theme.Transparent" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
答案 0 :(得分:0)
我不认为你的主要活动对“activity_nightmode_filter”有可见性,你不能把这个功能放在你的NightmodeFilter活动中吗?
答案 1 :(得分:0)
问题是您尝试获取引用的视图是活动视图本身,因此当您调用findViewById
时,它将返回空引用。
更改活动背景颜色的一种方法是:在onCreate
方法中,在致电setContentView(R.layout.activity_nightmode);
后,获取decorView并设置其背景颜色:
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#99000000"))
答案 2 :(得分:0)
任何更多的想法,都会受到关注。我有一个想法,如果我在透明活动中制作了一个透明的图像按钮,那么按钮点击会改变背景颜色而不会出现错误吗?