-1

I want to pass an integer from one activity to another and show the integer in textView Widget, there is no error in build but have 6 errors in logcat .I am beginner to android and I don't know so much. Please guide.

I want to go & send integer from one activity to another by one button.

MainActivity.java

    package com.ghermez.sibe.salary_usd;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import static java.lang.Integer.*;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button B1 =  findViewById(R.id.button1);
    B1.setOnClickListener(new OnClickListener() {

        EditText et_tusd = findViewById(R.id.tusd);
        int tusd = parseInt(et_tusd.getText().toString());\

        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,Main2Activity.class);
            startActivity(i);

            Intent i1 = new Intent(MainActivity.this,Main2Activity.class);
            i1.putExtra("i1",tusd);
            startActivity(i1);

        }

    });
}

}

Main2Activity.java

    package com.ghermez.sibe.salary_usd;

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

public class Main2Activity extends AppCompatActivity {

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

    Intent i1s = getIntent();
    Integer tusd = i1s.getIntExtra("i1",0);

    EditText editText_tusd = findViewById(R.id.tusd);
    editText_tusd.setText(tusd, TextView.BufferType.EDITABLE);
    }
}

My application doesn't run on virtual device. it has 6 errors in logcat :

logcat

04-21 16:43:13.526 5433-5433/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ghermez.sibe.salary_usd, PID: 5433
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ghermez.sibe.salary_usd/com.ghermez.sibe.salary_usd.MainActivity}: java.lang.NumberFormatException: For input string: ""
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:620)
    at java.lang.Integer.parseInt(Integer.java:643)
    at com.ghermez.sibe.salary_usd.MainActivity$1.<init>(MainActivity.java:26)
    at com.ghermez.sibe.salary_usd.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Salim13i
  • 159
  • 1
  • 1
  • 12

1 Answers1

0

It seems, that tusd string, which you pass to Main2Activity has wrong integer format.

Here you put it to Intent:

i1.putExtra("i1",tusd);

Here you get it from Intent:

Integer tusd = i1s.getIntExtra("i1",0);

And the logcat says that passed string has wrong integer format:

Caused by: java.lang.NumberFormatException: For input string: ""

Try to check your tusd string in MainActivity.

Yamashiro Rion
  • 1,778
  • 1
  • 20
  • 31
  • i think this two line is main problems: `at com.ghermez.sibe.salary_usd.MainActivity$1.(MainActivity.java:26)` - `at com.ghermez.sibe.salary_usd.MainActivity.onCreate(MainActivity.java:22)` ------- which related to : `int tusd = parseInt(et_tusd.getText().toString());` and `B1.setOnClickListener(new OnClickListener()` method – Salim13i Apr 21 '18 at 17:43