2013-03-28 91 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

相关问题