I have two editText in the application and I need to convert the information from one to an int from string. I tried different methods:

String log = loginEditText.getText().toString(); Integer login = Integer.parseInt(log); 

I tried valueof () before this variant, but an error also pops up, that is, in fact, because of one input field, I cannot start an application. I use this method in which these two fields:

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. loginEditText = findViewById(R.id.email); populateAutoComplete(); error = findViewById(R.id.textView); passwordEditText = findViewById(R.id.password); passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) { attemptLogin(); return true; } return false; } }); String log = loginEditText.getText().toString(); Integer login = Integer.parseInt(log); String password = passwordEditText.getText().toString(); final AuthRequestBody body = new AuthRequestBody(login, password); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://0") .addConverterFactory(GsonConverterFactory.create()) .build(); ApiService api = retrofit.create(ApiService.class); final Call<AuthResponse> call = api.authUser(body); Button mEmailSignInButton = findViewById(R.id.email_sign_in_button); mEmailSignInButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { call.enqueue(new Callback<AuthResponse>() { @Override public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) { if(response.isSuccessful()) { AuthResponse serverAnswer = response.body(); assert serverAnswer != null; if (serverAnswer.isUserLoggedIn()) { // Авторизация прошла успешно // можно, например, перейти на другую активити Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); } else { // Авторизация не прошла error.setText("Error"); } } } @Override public void onFailure(Call<AuthResponse> call, Throwable t) { } }); //attemptLogin(); messages(); } }); mLoginFormView = findViewById(R.id.login_form); mProgressView = findViewById(R.id.login_progress); } 

Here is the error code:

 07-11 07:32:02.086 1929-1929/com.example.developer_4.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.developer_4.myapplication, PID: 1929 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.developer_4.myapplication/com.example.developer_4.myapplication.LoginActivity}: java.lang.NumberFormatException: For input string: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:533) at java.lang.Integer.parseInt(Integer.java:556) at com.example.developer_4.myapplication.LoginActivity.onCreate(LoginActivity.java:96) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

if I understand correctly, I’m sort of passing an empty string, but in fact I haven’t yet managed to transfer anything. If anyone knows what the problem is, I will be grateful for the help.

  • Where is the given code? - Enikeyschik
  • did not understand what means where? where do i use it? - Andrew Goroshko
  • Yes. In what method, class, etc. - Enikeyschik
  • corrected the question - Andrew Goroshko

1 answer 1

You understood quite correctly - to the Integer.parseInt(log); method Integer.parseInt(log); An empty string is passed. And it is empty because nothing is contained in the loginEditText field. The application performs onCreate() immediately after the start of the activation, as expected, and does not wait for something to appear in the input field. You need to transfer the processing of information to where it should be processed - in the onCLick() buttons, for example.

Ps. Apparently, in the loginEditText field loginEditText should be an email. And you want to get an integer from there. Is there an error here?

  • Well, I somehow use the standard login screen, but I was given an id to enter which is in int and I try to adjust this screen to fit my goals))) - Andrew Goroshko
  • @ AndrewGoroshko, if the answer has solved your problem, accept it (check mark to the left of the answer) - Jarvis_J
  • the answer helped, but another problem was created)))) as always shorter - Andrew Goroshko