Akash Jaiswal Akash Jaiswal - 2 months ago 11
Java Question

My app crashes when entering value to edit text as well as it doesn't display the correct value

I am new to Android Studio. I have gone through the official developer.android.com training and I decided to create a new and simple app called Grocery+ in which user will enter the price and quantity of particular item and app will display total sum.

enter image description here

I have done all UI based work then today I switched to programming. I am an experienced programmer of Java. I have also done all the work in it but:

1- my app crashes when I try to enter the first .

enter image description here

Then I have to enter any other value first then first value.

2- even after above hack my app doesn't display anything on 'grand total

Plese help :(((

package com.amostrone.akash.grocery;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

static int Quantity[] = new int[4];
static float Price[] = new float[4];
public static double total=0;
static TextView txtValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtValue = (TextView) findViewById(R.id.txtExample);
}
/////////// QUANTITY
public void input_Quantity(View view) {
EditText x = (EditText) findViewById(R.id.editQuantity);
calc();
}
public void input_Quantity2(View view) {
EditText x = (EditText) findViewById(R.id.editQuantity2);
Quantity[1] = Integer.parseInt(x.toString());
calc();
}
public void input_Quantity3(View view) {
EditText x = (EditText) findViewById(R.id.editQuantity3);
Quantity[2] = Integer.parseInt(x.toString());
calc();
}
public void input_Quantity4(View view) {
EditText x = (EditText) findViewById(R.id.editQuantity4);
Quantity[3] = Integer.parseInt(x.toString());
calc();
}
public void input_Quantity5(View view) {
EditText x = (EditText) findViewById(R.id.editQuantity5);
Quantity[4] = Integer.parseInt(x.toString());
calc();
}
/////////////// Quantity



////////////// Price
public void input_Price(View view) {
EditText x = (EditText) findViewById(R.id.editPrice);
Price[0] = Float.parseFloat(x.toString());
calc();
}
public void input_Price2(View view) {
EditText x = (EditText) findViewById(R.id.editPrice2);
Price[1] = Float.parseFloat(x.toString());
calc();
}
public void input_Price3(View view) {
EditText x = (EditText) findViewById(R.id.editPrice3);
Price[2] = Float.parseFloat(x.toString());
calc();
}
public void input_Price4(View view) {
EditText x = (EditText) findViewById(R.id.editPrice4);
Price[3] = Float.parseFloat(x.toString());
calc();
}
public void input_Price5(View view) {
EditText x = (EditText) findViewById(R.id.editPrice5);
calc();
}
///////////// Price

/////////////// Calculate
public static void calc()
{
for(int i=0;i<=4;i++)
total += (Quantity[i] * Price[i]);
String str = Double.toString(total);
txtValue.setText(str);
}
////////////// Calculate }

Answer

Looking at your code it appears your are triggering EditText value get and calculation on click event (via xml). This does not work this way as they are triggered immediately. One of the approaches to solve this problem is to go the TextWatcher route. Check the below code built around that, I also refactored it a bit (should be lesser prone to memory leaks now):

public double mTotal;
    private TextView mTextView;

    private EditText[] mQuantityEditTexts = new EditText[5];
    private EditText[] mPriceEditTexts = new EditText[5];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.txtExample);

        mQuantityEditTexts[0] = (EditText) findViewById(R.id.editQuantity);
        mQuantityEditTexts[1] = (EditText) findViewById(R.id.editQuantity2);
        mQuantityEditTexts[2] = (EditText) findViewById(R.id.editQuantity3);
        mQuantityEditTexts[3] = (EditText) findViewById(R.id.editQuantity4);
        mQuantityEditTexts[4] = (EditText) findViewById(R.id.editQuantity5);

        mPriceEditTexts[0] = (EditText) findViewById(R.id.editPrice);
        mPriceEditTexts[1] = (EditText) findViewById(R.id.editPrice2);
        mPriceEditTexts[2] = (EditText) findViewById(R.id.editPrice3);
        mPriceEditTexts[3] = (EditText) findViewById(R.id.editPrice4);
        mPriceEditTexts[4] = (EditText) findViewById(R.id.editPrice5);

        for (int i = 0; i < 5; i++) {
            mQuantityEditTexts[i].addTextChangedListener(mTextWatcher);
            mPriceEditTexts[i].addTextChangedListener(mTextWatcher);
        }
    }

    private TextWatcher mTextWatcher = 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) {
            calc();
        }


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

    public void calc()
    {
        try {
            for (int i = 0; i <= 4; i++)
                mTotal += Integer.parseInt(mQuantityEditTexts[i].getText().toString()) *
                        Double.parseDouble(mPriceEditTexts[i].getText().toString());

            String str = Double.toString(mTotal);
            mTextView.setText(str);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "WTF! Enter valid numbers!", Toast.LENGTH_SHORT).show();
        }
    }
Comments