【问题标题】:Screen brightness control program屏幕亮度控制程序
【发布时间】:2011-09-12 03:33:26
【问题描述】:

我是android开发的初学者,确切地说,是在开发中。 我开始学习为 android 开发,并想做这个练习: 编写一个小程序,将亮度更改为三个不同的级别:电流-低-高。 在编写完我的代码和所有内容之后,我无法让它运行,每次运行它时,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);
        }

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




}

}

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

【问题讨论】:

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

标签: android brightness


【解决方案1】:

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

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

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




}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多