2013-03-28 59 views
1

開始在控制檯此評論後設備模擬器-5554 活動com.example.converter.MainActivity它讓我在那個不幸的是轉換器已經停止運行錯誤。不幸的應用程序已經停止

請問我該怎麼辦?

public class mainactivity extends Activity { 

private EditText text; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    // This method is called at button click because we assigned the name to the 
    // "OnClick property" of the button 
    public void onClick(View view) { 
    switch (view.getId()) { 
    case R.id.button1: 
     RadioButton celsiusButton = (RadioButton) findViewById(R.id.radioButton1); 
     RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radioButton2); 
     if (text.getText().length() == 0) { 
     Toast.makeText(this, "Please enter a valid number", 
      Toast.LENGTH_LONG).show(); 
     return; 
     } 

     float inputValue = Float.parseFloat(text.getText().toString()); 
     if (celsiusButton.isChecked()) { 
     text.setText(String 
      .valueOf(convertFahrenheitToCelsius(inputValue))); 
     celsiusButton.setChecked(false); 
     fahrenheitButton.setChecked(true); 
     } else { 
     text.setText(String 
      .valueOf(convertCelsiusToFahrenheit(inputValue))); 
     fahrenheitButton.setChecked(false); 
     celsiusButton.setChecked(true); 
     } 
     break; 
    } 
    } 

    // Converts to celsius 
    private float convertFahrenheitToCelsius(float fahrenheit) { 
    return ((fahrenheit - 32) * 5/9); 
    } 

    // Converts to fahrenheit 
    private float convertCelsiusToFahrenheit(float celsius) { 
    return ((celsius * 9)/5) + 32; 
    } 
} 
+0

logcat請僅填寫相關代碼。另外,我沒有看到'onClick()'被綁定到任何按鈕。那段代碼在哪裏? – SudoRahul 2013-03-28 06:25:34

+0

是否在佈局中將onClick指定爲onClick方法? – 2013-03-28 06:25:45

+0

只有一個'。'和'?'做這項工作。不要使用像'...'和'????????'這樣的聊天語言 – 2013-03-28 06:27:03

回答

1

您忘記初始化text EditText。

我認爲你是這裏的NullPointerException if (text.getText().length() == 0) {

所以在使用前初始化這個樣子。

text = (EditText)findViewById(R.id.yourTextViewId); 
+0

notolvednot它顯示我在代碼中的錯誤爲 – user2218527 2013-03-28 06:42:13

+0

float inputValue = Float.parseFloat(text.getText()。toString()); – user2218527 2013-03-28 06:42:29

+0

作爲刪除這個,如果我刪除這然後它顯示我錯誤的完整代碼 – user2218527 2013-03-28 06:43:42

相關問題