Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 屏幕亮度控制程序_Android_Brightness - Fatal编程技术网

Android 屏幕亮度控制程序

Android 屏幕亮度控制程序,android,brightness,Android,Brightness,我是android开发的初学者,确切地说,我正在开发。 我开始学习为android开发,并想做以下练习: 写一个小程序来改变亮度到三个不同的水平:电流低高。 在写了我的代码和所有东西之后,我无法让它运行,每次我运行它时,力就会关闭。请帮我找出我的错误( 我的代码: package com.dummies.android.helloandroid; import android.app.Activity; import android.os.Bundle; import android.view

我是android开发的初学者,确切地说,我正在开发。 我开始学习为android开发,并想做以下练习: 写一个小程序来改变亮度到三个不同的水平:电流低高。 在写了我的代码和所有东西之后,我无法让它运行,每次我运行它时,力就会关闭。请帮我找出我的错误(

我的代码:

package com.dummies.android.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
     // MY BRIGHTNESS VARIABLES


 WindowManager.LayoutParams lp = getWindow().getAttributes();
 float fb = lp.screenBrightness;
 float lb = 0;
 float hb = 1;
 //////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   // MY CODE FROM HERE DOWN

    Button button1=(Button)findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        if(lp.screenBrightness==fb) {
            lp.screenBrightness=lb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==lb){
            lp.screenBrightness=hb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==hb){
            lp.screenBrightness=fb;
            getWindow().setAttributes(lp);
        }

    }
} );
    //////////////////////////////////////////////




}

}

请帮助我:(我需要做什么才能让它工作?

无论如何,我发现了一个可能是潜在问题的错误

WindowManager.LayoutParams lp=getWindow().getAttributes();

这一行是您的潜在问题。请在执行
setContentView(R.layout.main);

在构造窗口之前,不能执行
getWindow().getAttributes()

因此,您的代码将成为

package com.dummies.android.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
     // MY BRIGHTNESS VARIABLES


 WindowManager.LayoutParams lp;
 float fb;
 float lb = 0;
 float hb = 1;
 //////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lp = getWindow().getAttributes();
    fb = lp.screenBrightness;

   // MY CODE FROM HERE DOWN

    Button button1=(Button)findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        if(lp.screenBrightness==fb) {
            lp.screenBrightness=lb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==lb){
            lp.screenBrightness=hb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==hb){
            lp.screenBrightness=fb;
            getWindow().setAttributes(lp);
        }

    }
} );
    //////////////////////////////////////////////




}

}

请添加来自DDMS Logcat的堆栈跟踪。如果您有堆栈跟踪,我们非常乐意提供帮助。