-8

Code

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

public class MainActivity extends AppCompatActivity {
    EditText a;

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

        a = (EditText) findViewById(R.id.a);

        int i = Integer.parseInt(a.getText().toString());
  }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Exception

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoaib.demo/com.example.shoaib.demo.MainActivity}: java.lang.NumberFormatException: For input string: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) Caused by: java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:629) at java.lang.Integer.parseInt(Integer.java:652) at com.example.shoaib.demo.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:7117) at android.app.Activity.performCreate(Activity.java:7108) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)

Philipp
  • 468
  • 3
  • 24
shoaib
  • 1
  • 2
  • 2
    Check if a is null or not. Your application crashed probably because you got a null pointer exception in accessing a. It is probably null. Can you paste the backing xml for this activity? – Rajan Prasad Jul 19 '18 at 15:28
  • String null cannot be converted into int. The edit text might contain a null value so check it . – Raj Jul 19 '18 at 15:30
  • It would be nice to know what troubleshooting steps you tried first, or what was happening when the application crashed. You'll get more responses that way than just throwing us a log dump. :) – NickSentowski Jul 19 '18 at 15:36
  • You can't convert an empty `String` to `int`. Furthermore why do you set `android:inputType="textPersonName"` when you obviously need an `int`? – Philipp Jul 19 '18 at 15:39
  • 1
    Possible duplicate of [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) –  Jul 19 '18 at 15:56

2 Answers2

1

The reason you are getting this error is because the EditText is not created on the UI yet.

But, you need the EditText on the screen, along with some valid data before you can call Integer.parseInt.

You need to use some sort of interface, through which you would call Integer.parseInt. I suggest using a button.

If you don't want to add a button though, use this code:

a.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        int i = Integer.parseInt(a.getText().toString());
        //any operation involving i
        return true;
    }
});

Then, after you type some data into a, long press a, and see the magic!

Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
0

The problem is that you are trying to convert a non int (" " - since the edittext is probably empty) character into an int character. You can try this code below;

String input = a.getText().toString();

if (TextUtils.isDigitsOnly(input)) {
     i = Integer.parseInt(input);
     // do everything you want with your int here
}
Melvin Kent
  • 340
  • 3
  • 15