问题描述
我有,我有4个seekbars和4 textviews使陪伴每一个搜索栏的Android程序。现在,我试图让每个TextView中的内容等于seekbars的整数进展。这是我的code,因为它代表,我已经尝试了许多方法来正确地贯彻OnSeekBarChangeListener,但仍LogCat中表明,它为空时SB1-SB4其设置为OnSeekBarChangeListener OnSeekBarProgress。请帮我找出我的问题:(
公共类TippopotamusActivity延伸活动{
搜索栏SB1;
搜索栏SB2;
搜索栏SB3;
搜索栏SB4;
TextView的TV1;
TextView中TV2;
TextView的TV3;
TextView的TV4;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑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 =(搜索栏)findViewById(R.id.seekBar1);
SB2 =(搜索栏)findViewById(R.id.seekBar2);
SB3 =(搜索栏)findViewById(R.id.seekBar3);
SB4 =(搜索栏)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 =
新OnSeekBarChangeListener(){ 公共无效onProgressChanged(搜索栏S,INT进步,布尔型触摸){
如果(s.getId()== R.id.seekBar1)
{
tv1.setText(进度);
}
否则如果(s.getId()== R.id.seekBar2)
{
tv2.setText(进度);
}
否则如果(s.getId()== R.id.seekBar3)
{
tv3.setText(进度);
}
其他
{
tv4.setText(进度);
}
} 公共无效onStartTrackingTouch(搜索栏S){ } 公共无效onStopTrackingTouch(搜索栏S){ }
};
移动你的的setContentView(R.layout.main);
初始化的变量之前。你的的onCreate
方法应该是这样的。
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑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 =(搜索栏)findViewById(R.id.seekBar1);
SB2 =(搜索栏)findViewById(R.id.seekBar2);
SB3 =(搜索栏)findViewById(R.id.seekBar3);
SB4 =(搜索栏)findViewById(R.id.seekBar4); sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
}
I have an android program that I am making with 4 seekbars, and 4 textviews to accompany each seekbar. Right now, I am trying to make each textview's contents equal to the integer progress of the seekbars. This is my code as it stands, and I have tried many ways to properly implement the OnSeekBarChangeListener, but LogCat still shows that it is null when sb1-sb4 sets their OnSeekBarChangeListener to OnSeekBarProgress. Please help me identify my problem :(
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){
}
};
Move your setContentView(R.layout.main);
before initializing your variables. Your onCreate
method should look like this
/** 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);
}
这篇关于OnSeekBarChangeListener始终计算为NULL的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!