2011-09-12 123 views
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); 
     } 

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




} 

} 

請幫我:(什麼,我需要做的就是它的工作

+0

請從DDMS,logcat中的堆棧跟蹤添加。如果您有堆棧跟蹤,我們非常樂意提供幫助。 – PH7

回答

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

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




} 

}