从数字选择器中获取价值并将其放入文本视图中
Get value from numberpicker and putting it in a textview
// 我需要分别从所有这些数字选择器方法中获取 newVal,并将它们设置到文本视图中,但是当尝试从文本视图应用程序中获取值时 crash.can 你帮帮我。我怎样才能在它的 onValuechange 方法之外获得 numberpicker 值?
public class MainActivity extends AppCompatActivity {
ImageButton start,pause,reset;
TextView total,mintimer,sectimer,textView6,textView7,textView8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(ImageButton)findViewById(R.id.start);
pause=(ImageButton)findViewById(R.id.pause);
reset=(ImageButton)findViewById(R.id.reset);
total=(TextView)findViewById(R.id.total);
mintimer=(TextView)findViewById(R.id.mintimer);
sectimer=(TextView)findViewById(R.id.sectimer);
//this is how i tried to get textview value as a string. then the app
crashes
String a=textView6.getText().toString();
NumberPicker min = (NumberPicker) findViewById(R.id.min);
NumberPicker sec = (NumberPicker) findViewById(R.id.sec);
NumberPicker brk = (NumberPicker) findViewById(R.id.brk);
min.setMinValue(1);
min.setMaxValue(20);
min.setWrapSelectorWheel(true);
min.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
textView6=(TextView)findViewById(R.id.textView6);
textView6.setText(" "+newVal);
}
});
//beep sound breake
sec.setMaxValue(60);
sec.setMinValue(0);
sec.setWrapSelectorWheel(true);
sec.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVa2, int newVal2) {
textView7=(TextView)findViewById(R.id.textView7);
textView7.setText(" "+newVal2);
}
});
//Break
brk.setMaxValue(5);
brk.setMaxValue(60);
brk.setWrapSelectorWheel(true);
brk.setOnValueChangedListener(new NumberPicker.OnValueChangeListener(){
@Override
public void onValueChange(NumberPicker picker ,int oldVal3,int newVal3){
textView8=(TextView)findViewById(R.id.textView8);
textView8.setText(" "+newVal3);
}
});
}
}
textview6 是一个空对象。它没有 TextView 的实例。
// 我需要分别从所有这些数字选择器方法中获取 newVal,并将它们设置到文本视图中,但是当尝试从文本视图应用程序中获取值时 crash.can 你帮帮我。我怎样才能在它的 onValuechange 方法之外获得 numberpicker 值?
public class MainActivity extends AppCompatActivity {
ImageButton start,pause,reset;
TextView total,mintimer,sectimer,textView6,textView7,textView8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(ImageButton)findViewById(R.id.start);
pause=(ImageButton)findViewById(R.id.pause);
reset=(ImageButton)findViewById(R.id.reset);
total=(TextView)findViewById(R.id.total);
mintimer=(TextView)findViewById(R.id.mintimer);
sectimer=(TextView)findViewById(R.id.sectimer);
//this is how i tried to get textview value as a string. then the app
crashes
String a=textView6.getText().toString();
NumberPicker min = (NumberPicker) findViewById(R.id.min);
NumberPicker sec = (NumberPicker) findViewById(R.id.sec);
NumberPicker brk = (NumberPicker) findViewById(R.id.brk);
min.setMinValue(1);
min.setMaxValue(20);
min.setWrapSelectorWheel(true);
min.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
textView6=(TextView)findViewById(R.id.textView6);
textView6.setText(" "+newVal);
}
});
//beep sound breake
sec.setMaxValue(60);
sec.setMinValue(0);
sec.setWrapSelectorWheel(true);
sec.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVa2, int newVal2) {
textView7=(TextView)findViewById(R.id.textView7);
textView7.setText(" "+newVal2);
}
});
//Break
brk.setMaxValue(5);
brk.setMaxValue(60);
brk.setWrapSelectorWheel(true);
brk.setOnValueChangedListener(new NumberPicker.OnValueChangeListener(){
@Override
public void onValueChange(NumberPicker picker ,int oldVal3,int newVal3){
textView8=(TextView)findViewById(R.id.textView8);
textView8.setText(" "+newVal3);
}
});
}
}
textview6 是一个空对象。它没有 TextView 的实例。