应用程序崩溃与editText

时间:2015-03-14 18:00:39

标签: java android if-statement android-activity double

我在这整个java / android编码时非常新,所以我从更有经验的人那里寻求帮助。当我回到editText标签的空间时,您是否碰巧知道为什么我的应用程序会一直崩溃?我相信它是因为双倍,当内容进入退格时它进入一个null,但是我一直在寻找一个if语句进入并使它成为可以,如果它是空的它没关系。带有编辑/文本视图的xml工作表是标记的基础,因此您知道。如果有人知道我怎么能让它不崩溃我会很感激:)谢谢

package xxxx.xxxx.xxxx;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import static java.lang.Math.*;
import java.util.ArrayList;
import java.util.Arrays;

public class TemperatureActivity extends Activity {
    EditText input;
    TextView output;
    String afterTextChanged = "";
    String beforeTextChanged = "";
    String onTextChanged = "";

        //    MainActivity instance = new MainActivity();
        public void onCreate (Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.temperature_layout);
//            setContentView(R.layout.activity_main);

            input = (EditText) findViewById(R.id.TemperatureInput);
            output = (TextView) findViewById(R.id.CelsiusOutput);
            input.addTextChangedListener(watch);

        }

        public TextWatcher watch = new TextWatcher() {
            @Override
            public void afterTextChanged(Editable arg0) {
                onTextChanged = input.getText().toString();
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                beforeTextChanged = input.getText().toString();
                // TODO Auto-generated method stub
            }


            @Override
            public void onTextChanged(CharSequence s, int a, int b, int c) {
                afterTextChanged = input.getText().toString();
                // TODO Auto-generated method stub
                //Parsing String to double
                double cercuils = Double.parseDouble(s.toString());
                double faherenheit = cercuils * 9 / 5 + 32;

                if (input == null) {
                    output.setText("ERROR");
                }



                output.setText(faherenheit + "");
                if (a == 9) {
                    Toast.makeText(getApplicationContext(),
                            "Maximum Limit Reached", Toast.LENGTH_SHORT).show();
                }
            }
        };
    }
//}

// Formula for C to F
//°C  x  9/5 + 32 = °F

// Formula for f to c
//37°C x  9/5 + 32 = 98.6°F

2 个答案:

答案 0 :(得分:0)

在这一行

double cercuils = Double.parseDouble(s.toString());

您将String解析为Double值。但也许你的String不是数字,也可能是空的。 如果是数字解析,你可以使用try catch

 double cercuils=0;
            try {
                 cercuils = Double.parseDouble(s.toString());
            } catch (NumberFormatException e) {
                 cercuils = 0;
            }

并使用

if(cercuils ==0)
{
output.setText("Please enter a number");
}else{
output.setText(faherenheit + "");
}

而不是

output.setText(faherenheit + "");

答案 1 :(得分:0)

您必须使用" 0"插入小数点,而不仅仅是"。" 。或者您可以通过添加" 0"以编程方式解决此问题。也。更新您的代码,如下所示

num2 = Double.parseDouble("0" + editText2.getText().toString());