public void Show(int n){
Intent in= new Intent(this, Results.class);
in.putExtra("Percent", n); // n is inetger
startActivity(in);`enter code here`
}
Intent intent= getIntent();
// int num = Integer.parseInt(String.valueOf(intent.getStringExtra("Percent")));
String num = intent.getStringExtra("Percent");
int num2= Integer.parseInt(num);
//ch.setText(num);
//String num= in.getStringExtra("Percent");
//int n=
//b.setProgress(num);
t.setText(num2);
You can recieve integer value like this:
Intent intent= getIntent();
int num = intent.getIntExtra("Percent");
You need to use int num intent.getInt("Percent");
Related
Why does this throw NullPointerException ?
In Redeem.java
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", yourInt);
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score = extras.getInt("intVariableName");
Try with:
Bundle extras = getIntent().getExtras();
String stringScore = extras.getString("intVariableName");
int score = Integer.parseInt(stringScore);
Or:
int score = intent.getIntExtra("intVariableName", 0);
You can also try this:
int yourInt = 200;
Intent myIntent = new Intent(Redeem.this, MainActivity.class);
myIntent.putExtra("intVariableName", String.valueOf(yourInt));
startActivity(myIntent);
In MainActivity.java
Bundle extras = getIntent().getExtras();
int score =Integer.parseInt(extras.getString("intVariableName"));
Use this:
getIntent().getIntExtra("intVariableName", 0); //where 0 is default value
I want to send int parameter in intent like this:
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
and in main class I get that parameter
Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));
but when I run my code it force closes!!!
You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));
Update:
int defaultValue = -1;// take any default value of your choice
String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue);
I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem
in other class i like this send parameter
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(list_sub.this,main.class);
i.putExtra("name", Name[position]);
i.putExtra("page", Ted[position]);
startActivity(i);
}
and in refresh class i get ted parameter
private void refresh(){
db.open();
int s = db.List_count("text", ID);
Name= new String[s];
Ted=new String[s];
for(int i=0;i<s;i++){
Name[i]=db.List_display_sub("text", ID, i);
Ted[i]=db.page_count("text", Name[i].toString())+"";
}
db.close();
}
Activity A
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
Activity B
Intent intent = getIntent();
name = intent.getStringExtra("name");
int page = intent.getIntExtra("page", 0);
where 0 is the default value.
I have some trouble with putExtras to an intent. Could you please review my code?
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra("EXT_CATEGORY", category);
startActivity(intent);
}
And in the ListActivity, I'm doing the following..
public static final String EXT_CATEGORY = "category";
int category = getIntent().getExtras().getInt(EXT_CATEGORY);
From this line
intent.putExtra("EXT_CATEGORY", category);
in your another activity String name should be same means:--
public static final String EXT_CATEGORY = "EXT_CATEGORY";
You did several mistakes, here is a overworker version of your code.
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra(EXT_CATEGORY, category);
startActivity(intent);
}
int defaultCat = -1;
public static final String EXT_CATEGORY = "category";
int category = getIntent().getIntExtra(EXT_CATEGORY,defaultCat); // Use default int if there is no extra
You should use this to get you info, not getExtras() :
int category = getIntent().getIntExtra("EXT_CATEGORY");
getExtras() returns additional Bundle of data. You need only one integer.
i pass int to next activity using this code
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
but when in this activity, it always display 0.
Intent intent = new Intent();
You are creating a new intent instead of using the one passed to your ActivityB. So use
Intent intent = getIntent();
instead;
use this int i = getIntent().getIntExtra("selectedType", 0);
try getIntent().getExtras().getInt("selectedType")
Try now,
int value = getIntent().getExtras().getInt("selectedType");
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
Intent intent = new getIntent();
^^^^^^^^^
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
Because you're creating a new intent and trying to get "selectedType" on it. But that intent has just been created, so it hasn't that value that you seek.
Try to getIntent() method to get your calling intent, which has your "selectedType" value...
Here's a snap:
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(value), Toast.LENGTH_LONG).show();
}
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
This is wrong. You are creating a new intent object. To get the intent object that was used to start this activity use getIntent() method.
Intent intent = getIntent();
int i = intent.getIntExtra("seelctedType", 0);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType",i);
startActivity(intent);
And receiving..
if (getIntent().getExtras().containsKey("selectedType")) {
int message = getIntent().getIntExtra("selectedType");
Toast.makeText(ReceiverActivity.this, "" + message, Toast.LENGTH_LONG)
.show();
}
I have a way that I know is not the best way of sending them over rite now but I'm sending them as strings and converting them to an Int on the reciver side, the problem is when I do the conversion it crashes on my phone. This is what I have on my sender side:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// hourly wage send
EditText editText = (EditText) findViewById(R.id.hourly_wage);
String message1 = editText.getText().toString();
intent.putExtra(MESSAGE_1, message1);
// ot wage send
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
String message2 = editText1.getText().toString();
intent.putExtra(MESSAGE_2, message2);
// hours per day send
EditText editText2 = (EditText) findViewById(R.id.hours_day);
String message3 = editText2.getText().toString();
intent.putExtra(MESSAGE_3, message3);
// start new activity
startActivity(intent);
And this is what is on my reciving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
String message1 = intent.getStringExtra(Options.MESSAGE_1);
String message2 = intent.getStringExtra(Options.MESSAGE_2);
String message3 = intent.getStringExtra(Options.MESSAGE_3);
// convert string to integer
int HW = Integer.valueOf(message1);
int OTW = Integer.valueOf(message2);
int HPD = Integer.valueOf(message3);
Ive tested everything and its the conversion that is causing the app to crash, i was hoping somebody could help me make it not crash or give me a whole new way sending an int to my second activity insted of sending a string and converting it.
Thank you!
=======================================================================
Here is my new code with all your help!
Sending:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, PayTracker.class);
// Gather text from text boxes
EditText editText = (EditText) findViewById(R.id.hourly_wage);
EditText editText1 = (EditText) findViewById(R.id.ot_wage);
EditText editText2 = (EditText) findViewById(R.id.hours_day);
//Create String from text
String message1 = editText.getText().toString();
String message2 = editText1.getText().toString();
String message3 = editText2.getText().toString();
//Convert String to Int
int HW = 0, OTW = 0, HPD = 0;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
// Send Integers to PayTracker.java
intent.putExtra(MESSAGE_HW, HW);
intent.putExtra(MESSAGE_OTW, OTW);
intent.putExtra(MESSAGE_HPD, HPD);
// start new activity
startActivity(intent);
}
Receiving side:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra(Options.MESSAGE_HW, 0);
int OTW = intent.getIntExtra(Options.MESSAGE_OTW, 0);
int HPD = intent.getIntExtra(Options.MESSAGE_HPD, 0);
// set textview
TextView textView = (TextView) this.findViewById(R.id.yourpay);
textView.setText(String.valueOf(HW));
}
You don't need to pass Integers as Strings to be converted back in the receiving Activity. Intents can hold Integers as well as Strings.
Simply add your data like you normally would:
int foo = 5;
Intent intent = new Intent(this, bar.class);
intent.putExtra("foobar", foo);
And then retrieve your int from the intent in the receiving Activity as follows.
Intent intent = getIntent();
int foo = intent.getIntExtra("foobar", 0);
Intents can hold more data than just Strings. In fact, take a look at the documentation. You can see that Intents can hold Longs, Doubles, Floats, Parcelables, etc.
Pass data from one activity to another activity "String" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent =getIntent();
Receivevalue =intent.getExtras().getString("TYPE");
(OR)
Receivevalue = getIntent().getExtras().getString("TYPE");
Pass data from one activity to another activity "Integer" value.useing intent
Activityone
Intent intent = new Intent(Activityone.this,Activitytwo.class)
intent.putExtra("TYPE", type);
startActivity(intent);
Activitytwo
Intent intent = getIntent();
value = intent.getIntExtra("TYPE", 0);
// Type = intent.getIntExtra(name, defaultValue);
Try something like this on the send side:
String message1 = editText.getText().toString();
intent.putExtra("MESSAGE_1", message1);
String message2 = editText1.getText().toString();
intent.putExtra("MESSAGE_2", message2);
String message3 = editText2.getText().toString();
intent.putExtra("MESSAGE_3", message3);
Receive side:
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
String mess1 = getIntent().getExtras().getString("MESSAGE_1");
String mess2 = getIntent().getExtras().getString("MESSAGE_2");
String mess3 = getIntent().getExtras().getString("MESSAGE_3");
}
}
You can achieve your goal using following code
In Your Calling Activity
Intent value = new Intent(YourCallingActivity.this,DestinationActivity.class);
value.putExtra("integerone", integeronevalue);
value.putExtra("integertwo", integertwovalue);
value.putExtra("integerthree", integertwovalue);
startActivity(value);
In Destination Activity
int integerone = getIntent().getExtras().getInt("integerone");
int integertwo = getIntent().getExtras().getInt("integertwo");
int integerthree = getIntent().getExtras().getInt("integerthree");
Hope it helps
Encapsulate the conversion part within try/catch block, because your strings may not be convertible into integer values.
int HW, OTW, HPD;
try{
HW = Integer.valueOf(message1);
OTW = Integer.valueOf(message2);
HPD = Integer.valueOf(message3);
}
catch(NumberFormatException nfe){
//do something else here
//for e.g. initializing default values to your int variables
}
*Or more appropriately, pass the integer values in your sending part and receive them as it is.