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

时间:2016-08-13 19:49:31

标签: java android android-studio

我要创建一个可以在Farhenheit和Celsius之间进行转换的应用程序,还可以实现一个显示接下来的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)

2 个答案:

答案 0 :(得分:3)

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

改变:

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

到:

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

或:

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

答案 1 :(得分:1)

看看这行代码:

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

变量temp声明为:

private EditText temp;

请注意,tempEditText。匹配此调用的String.valueOf()唯一版本是the one which takes an Object parameter。这与调用temp.toString()相同,后者打印类名和有关EditText对象的其他信息。

您需要检索用户输入的文字:

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