2017-07-08 69 views
0

//我需要分別從所有這些numberpickers方法中獲取newVal,並將它們設置爲textviews,但是嘗試從文本視圖應用程序crash.can中獲取值時你幫我 。我怎樣才能得到它的onValuechange方法外的numberpicker值?從numberpicker獲取值並將其放入textview

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); 


     } 

    }); 


} 

}

回答

0

textview6是一個空對象。它沒有TextView的實例。

+0

非常抱歉。我編碼在我的代碼,但仍然無法正常工作! –

+0

更新您的代碼請 – Jay

+0

對不起,我剛剛做到了! –

相關問題