我是编程和学习Android Studio的新手。我的Celcius-> Farenheit / Kelvin温度转换应用程序的代码存在一些问题,无法解决。
有更好的算法吗?
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private float etextvalue;
private float temp;
private String tempunit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.tempunit,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
EditText editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
final TextView textView = findViewById(R.id.textView3);
etextvalue = Float.parseFloat(editText.getText().toString());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(getTempunit().equals("*F (Farenheit)")){
float cftemp = (float) ((getEtextvalue()*1.8)+32);
setTemp(cftemp);
}
else if(getTempunit().equals("K (Kelvin)")){
float cktemp = (float) (getEtextvalue()+273.15);
setTemp(cktemp);
}
textView.setText(String.valueOf(getTemp()));
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String tempunit = adapterView.getItemAtPosition(i).toString();
if(tempunit.equals("*F (Farenheit)")){
Toast.makeText(adapterView.getContext(),tempunit+" : Selected",Toast.LENGTH_SHORT).show();
}
else if(tempunit.equals("K (Kelvin)")){
Toast.makeText(adapterView.getContext(),tempunit+" : Selected",Toast.LENGTH_SHORT).show();
}
setTempunit(tempunit);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
public float getEtextvalue(){
return etextvalue;
}
public void setTemp(float temp){
this.temp = temp;
}
public float getTemp() {
return temp;
}
public String getTempunit() {
return tempunit;
}
public void setTempunit(String tempunit) {
this.tempunit = tempunit;
}
}
还有
FATAL EXCEPTION: main
Process: com.example.apidatetime, PID: 4960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apidatetime/com.example.apidatetime.MainActivity}: java.lang.NumberFormatException: empty String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:452)
at com.example.apidatetime.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
在您的onCreate
中,有以下一行:
etextvalue = Float.parseFloat(editText.getText().toString());
首次创建活动时,editText将为空,因此editText.getText().toString()
将返回一个空字符串。
Float.parseFloat
无法解析空字符串,因此会引发异常。
据我所知,无需在etextvalue
中设置onCreate
,因此您只需删除该行即可解决问题。
当文本框更改时,您仍然需要实现更新etextvalue
的部分,但是该应用程序至少应该在此时启动。
您需要的一切都在链接的输出中。你应该