【发布时间】:2016-11-30 00:19:36
【问题描述】:
所以我有一个夜间模式过滤器应用程序,它在 mainactivity 上有透明活动,并且在屏幕中间有一个按钮,应该将透明活动更改为黑色“屏幕过滤器”用于夜间模式。我遇到的问题是 如果有人回答,将不胜感激 W / System.err:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.view.View.setBackgroundColor(int)' W/System.err:在 com.example.kenert.allinoneapp.Nightmode.turnNightOn(Nightmode.java:91) W/System.err:在 com.example.kenert.allinoneapp.Nightmode.nightmodeButtonClicked(Nightmode.java:78) W/System.err:在 java.lang.reflect.Method.invoke(Native Method) W/System.err:在 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>
【问题讨论】:
-
findViewById(R.id.activity_nightmode_filter).setBackgroundColor(Color.parseColor("#99000000")) 这是出现错误的行。