Passing numbers beetwen first and third activity Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Passing numbers beetwen first and third activity without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Hi im working on calculator and i have problem. Namely when i want to pass numbers from edit text from first activity in third activity i cant do anything with it. For example to first number i wanna add 3 and set show new value after add i cant. I know I got a bit confused but I hope you will know what’s going on

Here is 1st activity:

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

    btn1=(Button) findViewById(R.id.button);
    et1=(EditText) findViewById(R.id.editText);
    et2=(EditText) findViewById(R.id.editText2);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(MainActivity.this., Main2Activity.class);
            String age= et1.getText().toString();
            intent.putExtra("AGE", age);

            String weight= et2.getText().toString();
            intent.putExtra("WEIGHT", weight);

            startActivity(intent);
        }
        }
    };


}

}

2nd:

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

    btn1=(Button) findViewById(R.id.button2);
     btn1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent=new Intent(Main2Activity.this, Main3Activity.class);
             String age= getIntent().getStringExtra("AGE");
             intent.putExtra("AGE", age);

             String weight= getIntent().getStringExtra("WEIGHT");
             intent.putExtra("WEIGHT", weight);
             startActivity(intent);
         }
     });
}

}

3rd:

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

    tv1=(TextView) findViewById(R.id.textView);
    String weight= getIntent().getStringExtra("WEIGHT");
    String age= getIntent().getStringExtra("AGE");




    double x= age*21;
    tv1.setText(x);
}

}

Answer

There are few issues in the code, first, make sure you are really on the third activity. Because there are only two activities which I can see in the code: MainActivity, Main2Activity.

Another thing is in the 3rd activity’s onCreate, you are giving double value to the setText method and It will throw ResourceNotFound Exception because there is no method, which name is setText and accept double type of parameter. To make this work you need to convert double value to String, like this:

tv1.setText(String.valueOf(x));

Update

Also, your age variable is of String type, so to build a correct mathematical expression you need to convert the type of age variable to Integer: So :

double x= Integer.parseInt(age)*21;

We are here to answer your question about Passing numbers beetwen first and third activity - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji