屏幕亮度控制程序

时间:2011-09-12 03:33:27

标签: android brightness

我是android开发的初学者,确切地说,正在开发中。 我开始学习Android的开发,并想做这个练习: 写一个小程序将亮度改为三个不同的级别:current-low-high。 在编写了我的代码和所有内容后,我无法运行它,每次运行它时,FORCE CLOSE都会出现。请帮助我找到我的错误。 :(

我的代码:

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);
        }

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




}

}

请帮助我:(我需要做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

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

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);
        }

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




}

}