2012-02-05 42 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