0

Ok here´s the thing. Im working on my degree´s final project with android studio. One of my duties is to create an Arraylist whose max size has to be a number that the user introduces! i guess it looks simple but im stuck in some point, and i suspect is size value´s conversion between String and Int.

Specifically, im trying to take user´s number from and editText, convert to String getText().toString() then i use this to convert to int Integer.parseInt() but when i run it in device, app crashes instantly. I´ve tryed already with valueOf too, also changing int for Integer (im sorry, i dont rlly know the difference, i studied this all by my own from 0) and still get the instant close. Ive searched a lot here but i couldnt see a solution for something so specifically, so if someone could help me a bit, i would really appreciate that. Here is my code! hope it helps!

package com.example.listwithrecyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

 Integer num;
 String Snum;

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

    EditText ET1 = findViewById(R.id.editText);
    Snum = ET1.getText().toString();
    num = Integer.parseInt(Snum);

    //also if i replace Snum by a number like:"23" in this last line, it works, so i guess it could 
    //be the way i put that variable inside the parseInt().

}

public void SendList (View view){
    Intent A = new Intent(this, List.class);
    A.putExtra("size", num);
    startActivity(A);
}

}

Also if you know of a good library that can make stadistics math operations like typical deviations, arithmetic means, etc... Would be perfect for my final project.

Thank you so much !

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26
  • 1
    *"app crashes instantly"* What is the error. Since you haven't show error message and stacktrace, I'm going to close this question with instructions for how to find them (see link up top). Ask another question if you have trouble interpreting the error message. – Andreas Apr 28 '20 at 18:31
  • Do you have a stack trace (in logcat?). If yes, then [edit] your question and post the entire stack trace. – Abra Apr 28 '20 at 18:32
  • 3
    Doing this inside "onCreate" means that this happens when the Activity is created, so in this case when the app starts. This means that ET1.getText().toString() will be empty and Integer.parseInt("") is an error. Try moving this to an "onChange" listener on the EditText or when pressing a button and also, check if the input is valid – Tiberiu Dorian Moşescu Apr 28 '20 at 18:35
  • set the EditText to have a number type: android:inputType="number" – Alan Deep Apr 28 '20 at 18:35
  • @TiberiuDorianMoşescu Definitely that comment was the clue. I placed the conversion lines inside 'Sendlist' Method and now it works perfectly. Sorry for not including my logs, ill do it the next time. thank you so much! – Fernando SLG Apr 28 '20 at 18:52

0 Answers0