gpt4 book ai didi

java - 用于转换温度的 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 18:03:23 26 4
gpt4 key购买 nike

我要制作一个在华氏温度和摄氏度之间转换的应用程序,并实现一个下拉菜单(旋转器),显示接下来的 5 个温度及其相应的转换。

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Button;
import android.widget.Spinner;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

private EditText temp;
private RadioButton toC;
private RadioButton toF;
private Button ConvertButton;
private Spinner nextTempsSpinner;
private String tempText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Assigning the private Java variables to their XML counterparts

temp = (EditText) findViewById(R.id.editTemperature); //Text box where user enters temperature
toC = (RadioButton) findViewById(R.id.FtoC); //Radio Button user selects so that temperature will be interpreted as Fahrenheit and returned as Celsius
toF = (RadioButton) findViewById(R.id.CtoF); //Radio Button user selects so that temperature will be interpreted as Fahrenheit and returned as Celsius
ConvertButton = (Button) findViewById(R.id.GetConversion); //Button user will click to get the conversion
nextTempsSpinner = (Spinner) findViewById(R.id.spinMyTemps); //Spinner is the drop down menu where the next 5 conversions will appear

ConvertButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Double temporary, answer, temporaryTemperature;

String goingFrom = null; //Adding these so that we can use these strings in the dropdown spinner
String goingTo = null;

double value = Double.valueOf(temp.getText().toString());
if (toC.isChecked()) // If the radio button for converting from Fahrenheit to Celsius is selected this conversion will happen
{
value = UnitConverter.FahrenheitToCelsius(value);
goingFrom = "F";
goingTo = "C";
} else if (toF.isChecked()) // If the radio button for converting from Celsius to Fahrenheit is selected this conversion will happen
{
value = UnitConverter.CelsiusToFahrenheit(value);
goingFrom = "C";
goingTo = "F";
}
temp.setText(Double.valueOf(value).toString());

DecimalFormat twoDecimalFormat = new DecimalFormat("#.##");


temporaryTemperature = Double.parseDouble(String.valueOf(temp));

//Creating ArrayAdapter

List<String> listOfTemps = new ArrayList<String>();
ArrayAdapter getTempAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, listOfTemps);
getTempAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

for (int i = 0; i < 5; i++) {
answer = Double.parseDouble(twoDecimalFormat.format(temp));
temporary = temporaryTemperature + i;
tempText = temporary.toString();
tempText = tempText + " " + goingFrom + " = ";
tempText = tempText + answer.toString();
tempText = tempText + " " + goingTo;
listOfTemps.add(tempText);
}
nextTempsSpinner.setAdapter(getTempAdapter);
}
}

是的,我确实有一个名为 UnitConverter 的 Java 文件,它没有什么特别的,就是这个

public class UnitConverter {
public static double CelsiusToFahrenheit(double c){
return 32+c*9/5;
}

public static double FahrenheitToCelsius(double f){
return (f-32)*5/9;
}
}

转换部分确实有效,单选按钮、转换按钮也是如此。当我对微调器进行编码时,在 onClick 方法中的某个地方,它似乎已经失控,现在整个应用程序崩溃了。感谢大家的帮助。

编辑:logcat信息如下:

08-13 15:30:31.557 2398-2398/com.aly.converttemps I/art: Not late-enabling -Xcheck:jni (already on)

[ 08-13 15:30:31.610 1580: 1601 D/ ]
HostConnection::get() New Host Connection established 0x7f0863664ec0, tid 1601
08-13 15:30:31.632 2398-2398/com.aly.converttemps W/System: ClassLoader referenced unknown path: /data/app/com.aly.converttemps-2/lib/x86_64
08-13 15:30:36.881 2398-2398/com.aly.converttemps W/System: ClassLoader referenced unknown path: /data/app/com.aly.converttemps-2/lib/x86_64
08-13 15:30:37.106 2398-2548/com.aly.converttemps D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

[ 08-13 15:30:37.113 2398: 2398 D/ ]
HostConnection::get() New Host Connection established 0x7f086f9941c0, tid 2398


[ 08-13 15:30:37.152 2398: 2548 D/ ]
HostConnection::get() New Host Connection established 0x7f086f994440, tid 2548
08-13 15:30:37.163 2398-2548/com.aly.converttemps I/OpenGLRenderer: Initialized EGL, version 1.4

[ 08-13 15:30:37.198 1209: 2251 D/ ]
HostConnection::get() New Host Connection established 0x7f4b63d56500, tid 2251
08-13 15:30:45.585 2398-2398/com.aly.converttemps D/AndroidRuntime: Shutting down VM


--------- beginning of crash
08-13 15:30:45.586 2398-2398/com.aly.converttemps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aly.converttemps, PID: 2398
java.lang.NumberFormatException: Invalid double: "android.widget.EditText{1495b6d VFED..CL. .F....ID 42,42-592,144 #7f0b0003 app:id/editTemperature}"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:114)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.aly.converttemps.MainActivity.onClick(MainActivity.java:75)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

您正在将 editText 对象的引用作为 String 传递到 parseDouble(String) 中。

更改:

 temporaryTemperature = Double.parseDouble(String.valueOf(temp));

至:

 temporaryTemperature = Double.parseDouble(temp.getText().toString());

或:

 temporaryTemperature = Double.valueOf(temp.getText().toString())

关于java - 用于转换温度的 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38936404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com