Doodie Doodie - 9 months ago 34
Android Question

Android error java.lang.NumberFormatException

Full Error

FATAL EXCEPTION: main

Process: com.intent.explicitintent, PID: 9871
java.lang.NumberFormatException: For input string: "android.support.v7.widget.AppCompatEditText{c41d17d VFED..CL. ........ 80,80-400,148 #7f0d0075 app:id/numberOne}"
at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.parseInt(Integer.java:556)
at com.intent.explicitintent.MainActivity.add(MainActivity.java:40)
at com.intent.explicitintent.MainActivity$1.onClick(MainActivity.java:28)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


I am trying to add simple addition.But it doesn't work.
I have two EditText fields and one Button. I want the result to be shown in another activity when button is click in TextView

Here is my code

Activity One.

public class MainActivity extends AppCompatActivity {
EditText oneNumber;
EditText secondNumber;
Button button;

int e;
int b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneNumber = (EditText) findViewById(R.id.numberOne);
secondNumber = (EditText) findViewById(R.id.numberTwo);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
add(v);
}
});


}


public void add(View view) {


int a = Integer.parseInt(String.valueOf(oneNumber));
int b = Integer.parseInt(String.valueOf(secondNumber));
int result = a + b;
Intent intent = new Intent();
intent.putExtra("add", result);
intent.setClass(MainActivity.this, AnotherActivity.class);
startActivity(intent);





}


}

Second Activity

public class AnotherActivity extends AppCompatActivity {
TextView total;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
total = (TextView) findViewById(R.id.Total);
Bundle extras = getIntent().getExtras();
String result = extras.toString();
total.setText(result);






}

}


I think i mess up with getting the integer from EditText and getting the result inside textView in another activity

Any help?

Answer Source

problem is here:

int a = Integer.parseInt(String.valueOf(oneNumber));
int b = Integer.parseInt(String.valueOf(secondNumber));

it should be:

 int a = Integer.parseInt(oneNumber.getText().toString());
int b = Integer.parseInt(secondNumber.getText().toString());

oneNumber is an Editext . You need to get the string from it

In your second activity get the result using:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);
    total = (TextView) findViewById(R.id.Total);
    //get result
    Intent intent = getIntent();
    int result = intent.getIntExtra("add", 0);
    total.setText(String.valueOf(result));

  }