Reputation: 337
I am a beginner in android development are, to be exact, in development are. I was starting to learn developing for android, and wanted to do this exercise: write a small program to change the brightness to three different levels: current-low-high. and after writing my code and everything, I cannot get it to run, every time I run it, the FORCE CLOSE comes up. PLEASE HELP ME TO FIND MY ERROR. :(
my code:
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);
}
}
} );
//////////////////////////////////////////////
}
}
please help ME :( what do I need to do to get it to work?
Upvotes: 1
Views: 3791
Reputation: 3916
Anyway, I do spot one error that might be the potential problem.
WindowManager.LayoutParams lp = getWindow().getAttributes();
This line is your potential trouble. Move this to After you do setContentView(R.layout.main);
You can't do getWindow().getAttributes()
before window is constructed.
Thus, your code will become
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);
}
}
} );
//////////////////////////////////////////////
}
}
Upvotes: 2