如何在一个TextView中添加各种editText

时间:2019-04-09 00:10:39

标签: java android

我是编程新手,需要您的帮助,在editText txt50中插入数字时出现错误,应用崩溃了,请帮助我,我不知道这是什么错误:

代码:

public class Main2Activity extends AppCompatActivity {

   private EditText cinco, cien, doscientos, quinientos, mil, dosmil, cincomil, diezmil, veintemil, cincuentamil, cienmil;
    private TextView diezmob;

    public static final String nombres = "names";
    TextView txtBienvenido;

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

        txtBienvenido = (TextView) findViewById(R.id.txtbienvenido);
        String usuario = getIntent().getStringExtra("names");
        txtBienvenido.setText("¡Bienvenido(a) Hermano(a) " + usuario + "!");

        diezmob = (TextView) findViewById(R.id.txtdiezmob);
        cinco = (EditText) findViewById(R.id.txt50);
        cien = (EditText) findViewById(R.id.txt100);
        doscientos = (EditText) findViewById(R.id.txt200);
        quinientos = (EditText) findViewById(R.id.txt500);
        mil = (EditText) findViewById(R.id.txt1000);
        dosmil = (EditText) findViewById(R.id.txt2000);
        cincomil = (EditText) findViewById(R.id.txt5000);
        diezmil = (EditText) findViewById(R.id.txt10000);
        veintemil = (EditText) findViewById(R.id.txt20000);
        cincuentamil = (EditText) findViewById(R.id.txt50000);
        cienmil = (EditText) findViewById(R.id.txt100000);

        cinco.addTextChangedListener(new TextWatcher() {

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

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                    if ((cinco.toString().equals("")) && (!cinco.toString().equals(null)) && (cinco.toString().isEmpty() || (cinco.toString().length() >= 0))) {
                        double valor1 = Double.parseDouble((cinco.getText().toString()));
                        double valor2 = Double.parseDouble((cien.getText().toString()));
                        double valor3 = Double.parseDouble((doscientos.getText().toString()));
                        double valor4 = Double.parseDouble((quinientos.getText().toString()));
                        double valor5 = Double.parseDouble((mil.getText().toString()));
                        double valor6 = Double.parseDouble((dosmil.getText().toString()));
                        double valor7 = Double.parseDouble((cincomil.getText().toString()));
                        double valor8 = Double.parseDouble((diezmil.getText().toString()));
                        double valor9 = Double.parseDouble((veintemil.getText().toString()));
                        double valor10 = Double.parseDouble((cincuentamil.getText().toString()));
                        double valor11 = Double.parseDouble((cienmil.getText().toString()));
                        double suma = (valor1 * 50) + (valor2 * 100) + (valor3 * 200) + (valor4 * 500) + (valor5 * 1000) + (valor6 * 2000) + (valor7 * 5000) + (valor8 * 10000) + (valor9 * 20000) + (valor10 * 50000) + (valor11 * 100000);
                        String resultado = String.valueOf((int) suma);
                        diezmob.setText(String.valueOf(resultado));
                    } else {
                        diezmob.setText("0");
                    }
                }

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

LogCat:在editText txt50中插入数字时出错,应用程序崩溃:

  

java.lang.NumberFormatException:无效的双精度字符:“”

2 个答案:

答案 0 :(得分:1)

我使用此代码解决了此错误,如有任何错误,请说出来。

package com.example.josue.login;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Calendar;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private TextView diezmob;

    public static final String nombres = "names";
    TextView txtBienvenido;

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

        txtBienvenido = (TextView) findViewById(R.id.txtbienvenido);
        String usuario = getIntent().getStringExtra("names");
        txtBienvenido.setText("¡Bienvenido(a) Hermano(a) " + usuario + "!");

        diezmob = (TextView) findViewById(R.id.txtdiezmob);

        findViewById(R.id.btncalcular).setOnClickListener(this);
        findViewById(R.id.btncalcular5).setOnClickListener(this);
        findViewById(R.id.btncalcular10).setOnClickListener(this);
        findViewById(R.id.btncalcular15).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btncalcular:

                double cinco, cien, doscientos, quinientos, mil, dosmil, cincomil, diezmil, veintemil, cincuentamil, cienmil;

                String Cinco = ((EditText) findViewById(R.id.txt50)).getText().toString();
                String Cien = ((EditText) findViewById(R.id.txt100)).getText().toString();
                String Doscientos = ((EditText) findViewById(R.id.txt200)).getText().toString();
                String Quinientos = ((EditText) findViewById(R.id.txt500)).getText().toString();
                String Mil = ((EditText) findViewById(R.id.txt1000)).getText().toString();
                String Dosmil = ((EditText) findViewById(R.id.txt2000)).getText().toString();
                String Cincomil = ((EditText) findViewById(R.id.txt5000)).getText().toString();
                String Diezmil = ((EditText) findViewById(R.id.txt10000)).getText().toString();
                String Veintemil = ((EditText) findViewById(R.id.txt20000)).getText().toString();
                String Cincuentamil = ((EditText) findViewById(R.id.txt50000)).getText().toString();
                String Cienmil = ((EditText) findViewById(R.id.txt100000)).getText().toString();

                if (Cinco != null && !Cinco.equals("")) {
                    cinco =  Double.valueOf(Cinco);

                }else{
                    cinco = 0;

                }

                if (Cien != null && !Cien.equals("")){
                    cien =   Double.valueOf(Cien);

                }else{
                    cien=0;

                }

                if (Doscientos != null && !Doscientos.equals("")) {
                    doscientos =  Double.valueOf(Doscientos);

                }else{
                    doscientos=0;

                }

                if (Quinientos != null && !Quinientos.equals("")) {
                    quinientos = Double.valueOf(Quinientos);

                }else{
                    quinientos = 0;

                }
                if (Mil != null && !Mil.equals("")){
                    mil = Double.valueOf(Mil);

                }else{
                    mil = 0;

                }
                if (Dosmil != null && !Dosmil.equals("")) {
                    dosmil = Double.valueOf(Dosmil);

                }else {
                    dosmil = 0;

                }
                if (Cincomil != null && !Cincomil.equals("")) {
                    cincomil = Double.parseDouble(Cincomil);

                }else {
                    cincomil = 0;

                }
                if (Diezmil !=null && !Diezmil.equals("")) {
                    diezmil = Double.valueOf(Diezmil);

                }else {
                    diezmil = 0;

                }

                if (Veintemil != null && !Veintemil.equals("")) {
                    veintemil = Double.valueOf(Veintemil);

                }else {
                    veintemil = 0;

                }

                if (Cincuentamil != null && !Cincuentamil.equals("") ) {
                    cincuentamil = Double.valueOf(Cincuentamil);

                }else {
                    cincuentamil = 0;

                }

                if (Cienmil != null && !Cienmil.equals("")    ) {
                    cienmil = Double.valueOf(Cienmil);

                }else {
                    cienmil = 0;

                }



                double suma = (cinco * 50) + (cien * 100) + (doscientos * 200) + (quinientos * 500) + (mil * 1000) +
                            (dosmil * 2000) + (cincomil * 5000) + (diezmil * 10000) + (veintemil * 20000) + (cincuentamil * 50000) +
                            (cienmil * 100000);
                    String resultado = String.valueOf((int)(suma));
                    diezmob.setText(String.valueOf(resultado));

                break;

            case R.id.btncalcular5:

                Intent i = new Intent(this, Main5Activity.class);
                i.putExtra("dato",diezmob.getText().toString());
                startActivity(i);

                break;

            case R.id.btncalcular10:
                Intent ii = new Intent(this, Main5Activity.class);
                startActivity(ii);
                break;

            case
                    R.id.btncalcular15:
                Intent iii = new Intent(this, Main5Activity.class);
                startActivity(iii);
                break;

            default:
                break;
        }
    }
}

答案 1 :(得分:0)

让我们对此进行评估:

if ((cinco.toString().equals("")) && (!cinco.toString().equals(null)) && (cinco.toString().isEmpty() || (cinco.toString().length() >= 0))) {
                        double valor1 = Double.parseDouble((cinco.getText().toString()));

您要说的是(“ cinco是一个空字符串”)和((不是null)和((isEmpty(与空字符串相同))还是长度> = 0),然后解析double

我非常确定,只有当cinco为空时,才将其评估为true,这将导致NumberFormatException,因为您正尝试在cinco为空时将其解析为double。您将不得不使用try-catch块来处理异常:

try {
    double valor1 = Double.parseDouble((cinco.getText().toString()));
} catch (NumberFormatException e) {
    e.printStackTrace();
}

或构造if语句,如果该语句不允许为cinco为空,则构造该语句:

if(cinco.toString() != null && !(cinco.toString().isEmpty() {
    double valor1 = Double.parseDouble((cinco.getText().toString()));
} 

edit:您代码中的所有Double.parseDouble都会抛出NumberFormatExceptions,而不仅仅是cinco。那只是第一个,因此此答案适用于所有解析。