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

时间:2017-03-21 16:43:39

标签: 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);
            }
        };


    }

1 个答案:

答案 0 :(得分:3)

你说:

  

只有当我点击按钮envoyer时,我的两个编辑文本清空我的应用程序崩溃

问题出在这里:

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

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

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

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

我会做类似的事情:

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