У меня в приложении есть два editText и информацию из одного мне нужно конвертировать в int из string. Я пробовал разные методы:
String log = loginEditText.getText().toString();
Integer login = Integer.parseInt(log);
до этого варианта пробовал valueof() но тоже выскакивает ошибка, то есть по факту из-за одного поля для ввода я не могу запустить приложение.метод в котором использую эти два поля:
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);
}
вот код ошибки:
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)
если я правильно понял то я вроде как передаю пустую строку но по факту я ведь еще ничего не успел передать. Если кто знает в чем проблема то буду благодарен за помощь.