我有一个用4个搜索栏制作的android程序,每个搜索栏都带有4个textviews。现在,我正在尝试使每个textview的内容等于搜索栏的整数进度。这就是我的代码,并且我尝试了许多方法来正确实现OnSeekBarChangeListener,但是LogCat仍然显示,当sb1-sb4将其OnSeekBarChangeListener设置为OnSeekBarProgress时,它为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){
}
};
最佳答案
在初始化变量之前,先移动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);
}
关于java - 在Android中,OnSeekBarChangeListener始终为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9149009/