java - Android Java,为什么我的按钮使我的应用程序崩溃

标签 java android xml

为什么当我在 XML 中编辑文本为空并且单击按钮 envoyerListener 时,我的应用程序崩溃了?

您可以在下面找到Java代码,这可能不是那么好,但是应用程序运行良好。仅当我单击按钮 envoyer 且两个 EditText 为空时,我的应用程序才会崩溃。

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.text.Editable;
        import android.text.TextWatcher;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.content.Intent;

        public class MainActivity extends AppCompatActivity {

        private final String defaut = "result";

        EditText price = null;
        EditText rent = null;
        TextView result = null;
        Button buttenvoyer = null;
        Button buttrent = null;
        Button buttprice = null;
        Button buttinfo = null;
        Button buttclear = null;


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

            price = (EditText) findViewById(R.id.price);
            rent = (EditText) findViewById(R.id.rent);
            result = (TextView) findViewById(R.id.result);
            buttenvoyer = (Button) findViewById(R.id.buttcalculate);
            buttrent = (Button) findViewById(R.id.buttrent);
            buttprice = (Button) findViewById(R.id.buttprice);
            buttinfo = (Button) findViewById(R.id.buttinfo);
            buttclear = (Button) findViewById(R.id.buttclear);

            price.addTextChangedListener(textWatcher);
            rent.addTextChangedListener(textWatcher);
            buttenvoyer.setOnClickListener(envoyerListener);
            buttrent.setOnClickListener(rentListener);
            buttprice.setOnClickListener(priceListener);
            buttinfo.setOnClickListener(infoListener);
            buttclear.setOnClickListener(clearListener);
        }

        private TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int befor, int count) {
                result.setText(defaut);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        private OnClickListener envoyerListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                String p = price.getText().toString();
                String r = rent.getText().toString();

                float pValue = Float.valueOf(p);
                float rValue = Float.valueOf(r);

                float resultat = rValue * 100 / pValue;
                result.setText("the profit is " + String.valueOf(resultat) + " %");
            }
        };

        private OnClickListener rentListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent jumpage = new Intent(MainActivity.this, Rent.class);
                startActivity(jumpage);
            }
        };

        private OnClickListener priceListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent jumpage = new Intent(MainActivity.this, Price.class);
                startActivity(jumpage);
            }
        };

        private OnClickListener infoListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent jumpage = new Intent(MainActivity.this, Infoam.class);
                startActivity(jumpage);
            }
        };

        private OnClickListener clearListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                price.getText().clear();
                rent.getText().clear();
                result.setText(defaut);
            }
        };


    }

最佳答案

你说:

only when I click on the button envoyer with my two Edit text empty my application crash

所以问题就在这里:

String p = price.getText().toString();
String r = rent.getText().toString();

float pValue = Float.valueOf(p);
float rValue = Float.valueOf(r);

在您的监听器中,您不会检查这些字符串。如果它们为空(当您抓取空文本时会发生这种情况),那么您就没有要解析的值。结果它抛出 NumberFormatException .

当您获取文本 pr 时,您必须执行一些检查,因为您只会得到一个空字符串 ""无法转换为任何内容,请选择将 String 的值设置为 0,或选择将 float 值设置为 0。

我会做这样的事情:

float pValue;
if(p.isEmpty())
{
    pValue = 0;
}
else
{
    pvalue = Float.valueOf(p);
}

关于java - Android Java,为什么我的按钮使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42933294/

相关文章:

java - Android ListView : Do Not Show TextView When Null

java - Android 中的 SQLite : Foreign Keys and <table constraint> expected

java - 当在方法级别内部使用 @PreAuthorize 或 @PostAuthorize 时,Class.getAnnotations() 不会返回类级别注释的完整列表

android - 匹配父约束布局不匹配

AndroidViewClient:尾部不允许有内容

php - 使用 xpath 从 xml 中获取值

java - 读取存储在 GAE Blobstore 中的 zip 文件

java - 是否有像 vb.net 的 Do Until Loop 这样的 Java 内置结构?

java - 如何将 (JDBC) 结果集转换为 Hibernate?

android - 如何在 Android 10 中获取有关手机 IMEI 的信息?