0

Please help with this code

I have a button in the first activite I want when I press it to send text and number to another activity

But I want to display the text in TextView 1 and the number in TextView2 I succeeded in sending the text to its specified location but I have a problem sending the number

I experimented with many of the codes and the failures persisted. This is the last code you used to successfully send the text but failed to send the number

The code from the first activity:

Intent intent = new Intent( this, Order.class );
String keyIdentifer  = null;
intent.putExtra( "String", text );
intent.putExtra( "Int", price );
startActivity( intent );

The code from the second Activity:

TextView userName = (TextView)findViewById(R.id.the_order);
Intent iin= getIntent();
Bundle b = iin.getExtras();

if(b!=null)
{
  String j =(String) b.get("String");
  userName.setText(j);
}
TextView userName1 = (TextView)findViewById(R.id.price);
Intent ii= getIntent();
Bundle bb = ii.getExtras();

if(bb!=null)
{
  int jj =(int) bb.get("Int");
  userName1.setText(jj);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

0

get the int as:

int jj = yourintent.getIntExtra("Int");

You don't need to get the bundle first, this way is a wrap around the thing that you are doing and gives you the same value

One other error is that you are setting an int value inside the setText method.

This method accepts int but it expects the int value to be a resource identifier. Since you are passing a value which is not a resource id, it will crash saying resource not found.

try

userName1.setText(String.valueOf(jj));

Basically if you want to set the TextView value as an int, never forget to convert it to string. The error thrown is horrendously worded and doesn't remotely indicate that this is the actual issue.

Kushan
  • 5,855
  • 3
  • 31
  • 45
0
Intent intent = new Intent(this, Activity.class);
intent.putExtra("Int", 4);
startActivity(intent);

In the activity its going to...

Intent intent = getIntent;
int getInt = intent.getIntExtra("Int");
System.out.println(getInt);

set a testview:

textView.setText(String.valueOf(getInt);
letsCode
  • 2,774
  • 1
  • 13
  • 37
0

Your issue is you are setting an Int value in editText:

int jj =(int) bb.get("Int");
userName1.setText(jj);

Just do this when setting int value:

int jj =(int) bb.get("Int");
userName1.setText(jj+"");
MezzDroid
  • 560
  • 4
  • 11
0

just change

bb.get("Int");

to

bb.getInt("Int");
Furqan Khan
  • 528
  • 4
  • 14