2015-09-25 44 views
0

爲什麼每當我嘗試更改EditText的文本時,我的應用程序總是崩潰?我試過 celc =(EditText)findViewById(R.id.cel); far =(EditText)findViewById(R.id.fa); Ran =(EditText)findViewById(R.id.ran); kelvin =(EditText)findViewById(R.id.kev);
celc.addTextChangedListener(新TextWatcher(){設置EditText的文本時應用程序崩潰

 @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      String value = s.toString() ; 
      double c1 = Double.parseDouble(value) ; 
      double f1 = (32+((9.0/5)*c1)); 
      double r1 = f1+460 ; 
      double k1 = c1 + 273.0 ; 

      far.setText(f1+""); 
      Ran.setText((r1 + "")); 
      kelvin.setText(k1+""); 

     } 
    }); but it doesn't work. 
+5

如何赫克是我們應該知道既不代碼不具有堆棧跟蹤?所以我會和「gremlins做到這一點」一起去。 –

+0

請提供一個[最簡單,完整且可驗證的示例](http://stackoverflow.com/help/mcve)來展示您的問題。在這種情況下,請發佈[與您的崩潰相關的Java堆棧跟蹤](https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this),以及堆棧跟蹤中引用的代碼。 – CommonsWare

+0

把你的logcat錯誤。 –

回答

6
  1. 聲明的EditText在xml文件
  2. 找到的EditText在活動
  3. 在EditText上

設置文本,如果你檢查的EditText的文檔,你會發現一個setText()方法。它需要一個字符串和一個TextView.BufferType。例如:

EditText editText =  (EditText)findViewById(R.id.edit_text); 
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

或使用+,字符串連接運算符:

ed = (EditText) findViewById (R.id.box); 
    int x = 10; 
    ed.setText(""+x); 

String.valueOf(int): 
ed.setText(String.valueOf(x)) 
0

也許你沒有使用你的java對象鏈接視圖對象

您與findViewById方法使這個

您的佈局文件。:

<EditText ... android:id="@+id/edittext" /> 

JAV守則:在類定義:

private EditText myEditText; 

和方法的onCreate:

 edittext = (EditText) findViewById(R.id.editText); 

最好的問候!

相關問題