-1

public class MainActivity extends AppCompatActivity {

EditText actv;
Button bt, total;
ListView lt;
String getInput;
ArrayAdapter adapter;
TextView tv;

int num, sum =0; final ArrayList addArray = new ArrayList();

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



    actv = (EditText) findViewById(R.id.autoCompleteTextView) ;
   bt = (Button) findViewById(R.id.button);
    total = (Button)findViewById(R.id.btt) ;
    tv = (TextView) findViewById(R.id.textView);
    lt = (ListView) findViewById(R.id.listView);

// button to create listview that contains numbers

bt.setOnClickListener(new View.OnClickListener() { @Override

        public void onClick(View view) {

             getInput = actv.getText().toString();

             num = Integer.valueOf(getInput);
             addArray.add(num);

             adapter = new ArrayAdapter(MainActivity.this,
                     android.R.layout.simple_list_item_1,addArray);

             lt.setAdapter(adapter);

            ((EditText) findViewById(R.id.autoCompleteTextView)).setText("");
   }

});

// another button to calculate the total of all numbers

    total.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i <addArray.size() ; i++) {
                sum = sum + addArray.get(i);
            }
            tv.setText(sum);
        }
    });

when i enter multiple numbers in edittext it shows in listview as expected but app crashes and does not perform addition after clicking the button

  • 2
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Jun 14 '19 at 16:48
  • Hey Ajay, will be easier if you paste your crash log as well – Shermano Jun 14 '19 at 17:23
  • 2019-06-15 00:35:00.752 29989-29989/com.example.actvinlt E/xample.actvinl: Invalid ID 0x0000005a. 2019-06-15 00:35:00.752 29989-29989/com.example.actvinlt D/AndroidRuntime: Shutting down VM 2019-06-15 00:35:00.754 29989-29989/com.example.actvinlt E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.actvinlt, PID: 29989 android.content.res.Resources$NotFoundException: String resource ID #0x5a – Ajay Shingade Jun 14 '19 at 19:08
  • @Shermano Plz check this logs and thank you so much for your help – Ajay Shingade Jun 14 '19 at 19:12
  • From the crash log, I think your problem is here: `tv.setText(sum);` where sum is treated as a String resource ID. Therefore change it to either `tv.setText(String.valueOf(sum));` or `tv.setText("" + sum);`. Hope that helps! – i_A_mok Jun 17 '19 at 14:01
  • Yeah i checked and it resolves the problem....thank u so much @I_A_Mok – Ajay Shingade Jun 18 '19 at 08:26

1 Answers1

0

You should never assume that your arrays are valid without checking them first... try adding some safety measures:

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

actv = (EditText) findViewById(R.id.autoCompleteTextView) ;
bt = (Button) findViewById(R.id.button);
total = (Button)findViewById(R.id.btt) ;
tv = (TextView) findViewById(R.id.textView);
lt = (ListView) findViewById(R.id.listView);

addArray = new ArrayList();

...

    public void onClick(View view) {

         getInput = actv.getText().toString();

         num = Integer.valueOf(getInput);

         if (addArray !=null) {
             addArray.add(num);


             adapter = new ArrayAdapter(MainActivity.this,
                 android.R.layout.simple_list_item_1,addArray);

             lt.setAdapter(adapter);
         }

        ((EditText) findViewById(R.id.autoCompleteTextView)).setText("");

}

...

total.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if ((addArray != null) && (addArray.size() > 0)) {
            for (int i = 0; i <addArray.size() ; i++) {
                sum = sum + addArray.get(i);
            }
            tv.setText(sum);
        }
    }
});
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13