2012-02-05 23 views
1

我有一個Android程序,我使用4個seekbars和4個textviews來伴隨每個seekbar。現在,我試圖讓每個textview的內容等於seekbars的整數進度。這是我的代碼,並且我嘗試了很多方法來正確實現OnSeekBarChangeListener,但是當sb1-sb4將OnSeekBarChangeListener設置爲OnSeekBarProgress時,LogCat仍然顯示它爲空。請幫我找出我的問題:(OnSeekBarChangeListener總是在Android中計算爲null

public class TippopotamusActivity extends Activity { 
    SeekBar sb1; 
    SeekBar sb2; 
    SeekBar sb3; 
    SeekBar sb4; 
    TextView tv1; 
    TextView tv2; 
    TextView tv3; 
    TextView tv4; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    tv1 = (TextView) findViewById(R.id.textView1); 
    tv2 = (TextView) findViewById(R.id.textView2); 
    tv3 = (TextView) findViewById(R.id.textView3); 
    tv4 = (TextView) findViewById(R.id.textView4); 

    sb1 = (SeekBar) findViewById(R.id.seekBar1); 
    sb2 = (SeekBar) findViewById(R.id.seekBar2); 
    sb3 = (SeekBar) findViewById(R.id.seekBar3); 
    sb4 = (SeekBar) findViewById(R.id.seekBar4); 

    sb1.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb2.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb3.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb4.setOnSeekBarChangeListener(OnSeekBarProgress); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

OnSeekBarChangeListener OnSeekBarProgress = 
     new OnSeekBarChangeListener() { 

     public void onProgressChanged(SeekBar s, int progress, boolean touch){ 
      if(s.getId() == R.id.seekBar1) 
      { 
       tv1.setText(progress); 
      } 
      else if(s.getId() == R.id.seekBar2) 
      { 
       tv2.setText(progress); 
      } 
      else if(s.getId() == R.id.seekBar3) 
      { 
       tv3.setText(progress); 
      } 
      else 
      { 
       tv4.setText(progress); 
      } 
     } 

     public void onStartTrackingTouch(SeekBar s){ 

     } 

     public void onStopTrackingTouch(SeekBar s){ 

     } 
     }; 

回答

1

初始化的變量之前將您的setContentView(R.layout.main);。你onCreate方法應該是這樣的

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.textView1); 
    tv2 = (TextView) findViewById(R.id.textView2); 
    tv3 = (TextView) findViewById(R.id.textView3); 
    tv4 = (TextView) findViewById(R.id.textView4); 

    sb1 = (SeekBar) findViewById(R.id.seekBar1); 
    sb2 = (SeekBar) findViewById(R.id.seekBar2); 
    sb3 = (SeekBar) findViewById(R.id.seekBar3); 
    sb4 = (SeekBar) findViewById(R.id.seekBar4); 

    sb1.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb2.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb3.setOnSeekBarChangeListener(OnSeekBarProgress); 
    sb4.setOnSeekBarChangeListener(OnSeekBarProgress); 
} 
+0

精良,APK將實際安裝和現在運行,但是當我嘗試移動SeekBar,應用程序崩潰,你碰巧有什麼線索,我做錯了什麼? – mchao92 2012-02-05 12:08:34

+0

@ mchao92:可能是一些NullPointerException。什麼是logcat中的stacktrace? – 2012-02-05 12:23:10

+0

@ mchao92。嘗試使用tv3.setText(「」+ progress);因爲setText需要字符串輸入沒有解決問題,發送你正在獲得的異常logcat。 – 2012-02-05 12:23:44