1

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

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




}

}

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

4

1 回答 1

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

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




}

}
于 2011-09-12T03:49:47.203 回答