在android studio中编写用于以编程方式更改亮度的代码时,setContentView,getContentResolver,ChangeBright,findViewById和getWindow都是红色的。它说不能解决方法。为什么会这样?
private SeekBar brightbar;
private int brightness;
private ContentResolver Conresolver;
private Window window;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab3);
brightbar = (SeekBar) findViewById(R.id.ChangeBright);
Conresolver = getContentResolver();
window = getWindow();
brightbar.setMax(255);
brightbar.setKeyProgressIncrement(1);
try
{
brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException e)
{
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
brightbar.setProgress(brightness);
brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar seekBar)
{
System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if(progress<=20)
{
brightness=20;
}
else
{
brightness = progress;
}
答案 0 :(得分:0)
您确定您的代码是否扩展了活动?