2011-09-12 6 views
1

私はアンドロイド開発の初心者であり、正確には開発中です。 私はアンドロイドのための開発を学び始めていたし、この練習をしたかった: 3つの異なるレベルに明るさを変更する小さなプログラムを書く:現在の低 - 高。 と私のコードとすべてを書いた後、私はそれを実行することはできません、私はそれを実行するたびに、強制終了が表示されます。私のエラーを見つけるのを助けてください。 :(画面の明るさコントロールプログラム

私のコード:

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

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

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




} 

} 
関連する問題