Can't pass string value - java

I want to create a simple calulator, and I get value of second activity of 0 no matter what I write in text View.
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
startActivity(intent);
2 activity :
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int wpis = 0;
if (extras != null) {
wpis = extras.getInt("wpis1");
}
int wpis2 = 0;
if (extras != null) {
wpis2 = extras.getInt("wpis2");
}
TextView tv = findViewById(R.id.result);
tv.setText(String.valueOf(wpis) + String.valueOf(wpis2));

As mention in the comment. You also need to link the Bundle variable with the Intent variable. See the follow link: https://zocada.com/using-intents-extras-pass-data-activities-android-beginners-guide/
//create a Bundle object
Bundle extras = new Bundle();
//Adding key value pairs to this bundle
//there are quite a lot data types you can store in a bundle
extras.putString("USER_NAME","jhon Doe");
extras.putInt("USER_ID", 21);
extras.putIntArray("USER_SELCTIONS", [1, 2, 3, 4, 5]);
...
//create and initialize an intent
Intent intent = new Intent(this, NextActivity.class);
//attach the bundle to the Intent object
intent.putExtras(extras);
//finally start the activity
startActivity(intent);
So your code needs to be:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);

Just change your code to:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
You forgot about putExtras() method!

Related

Failed to pass value from one activity from another (only null value passed)

I tried to pass variable from one activity to another. In the main activity I managed to call back the values using Toast message.
MainActivity.java
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View w) {
subreg = spinner_subregion.getSelectedItem().toString();
reg = spinner_region.getSelectedItem().toString();
pettype = spinner_pet.getSelectedItem().toString();
petnumber = spinner_num.getSelectedItem().toString();
Intent intent = new Intent(MainActivity, Result.class);
intent.putExtra("subregion", subreg);
intent.putExtra("region", reg);
intent.putExtra("petnum", petnumber);
intent.putExtra("pet", pettype);
startActivity(intent);
}
}
However, when I passed the value to Result.java, it only returns null. I tried searching for solutions and still does not work for me. Anyone knows how can I pass the data?
Result.java
Bundle extras = getIntent().getExtras();
if (extras!=null){
subreg = extras.getString("subreg");
reg = extras.getString("reg");
pettype = extras.getString("pettype");
petnumber = extras.getString("petnumber");
}
Try this
MainActivity.java
Intent myIntent = new Intent(this, NewActivity.class);
intent.putExtra("subregion", subreg);
intent.putExtra("region", reg);
intent.putExtra("petnum", petnumber);
intent.putExtra("pet", pettype);
startActivity(myIntent)
Result.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
if(intent ==null){
........
.......
}else{
String subregion= intent.getStringExtra("subregion");
String region= intent.getStringExtra("region");
String petnum= intent.getStringExtra("petnum");
String pet= intent.getStringExtra("pet");
}
}
This is my code after changing it.
MainActivity.java
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View w) {
subreg = spinner_subregion.getSelectedItem().toString();
reg = spinner_region.getSelectedItem().toString();
pettype = spinner_pet.getSelectedItem().toString();
petnumber = spinner_num.getSelectedItem().toString();
Intent intent = new Intent(Pet_sitting.this, Pet_sitting_result.class);
intent.putExtra("subreg", subreg);
intent.putExtra("reg", reg);
intent.putExtra("pettype", pettype);
intent.putExtra("petnumber", petnumber);
startActivity(intent);
}
});
Result.java
Intent intent = getIntent();
if (intent == null){
Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
}
else{
subreg= intent.getStringExtra("subreg");
reg= intent.getStringExtra("reg");
pettype = intent.getStringExtra("pettype");
petnumber = intent.getStringExtra("petnumber");
Toast.makeText(getApplicationContext(),subreg + " " + reg + " " + pettype + " " + petnumber,Toast.LENGTH_SHORT).show();
}
As i can see you are using a Spinner in your code.
So it's Obvious a spinner can give you only one value at a time after Spinning the wheel...
you need to set the Code inside in null value Exception...
like...if it's null don't pass empty value inside intent
maybe it's worked...if it's then reply
MainActivity.java
if(subreg != null){
reg = spinner_region.getSelectedItem().toString();
intent.putExtra("reg", reg);
}
startActivity(intent);
Rsult.java
Intent intent = getIntent();
if (intent == null){
Toast.makeText(getApplicationContext(),"Null value passed",Toast.LENGTH_SHORT).show();
}else{
reg= intent.getStringExtra("reg");
Toast.makeText(getApplicationContext(),reg.toString,Toast.LENGTH_SHORT).show();
}

How to add/pass multiple values to Intent object?

i have two EditText objects in my first activity. i want both their values when i go to the next activity.
Lets assume the EditText objects are inp1, inp2 and they can only accept numbers.
please mention how i can add their values to int Intent object and how i will extract their values in my next activity's .java file.
Here we go, your code will look like,
Sender Side:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName1", intValue1);
myIntent.putExtra("intVariableName2", intValue2);
startActivity(myIntent);
Receiver Side:
Intent mIntent = getIntent();
int intValue1 = mIntent.getIntExtra("intVariableName1", 0);
int intValue2 = mIntent.getIntExtra("intVariableName2", 0);
Hope it helps.
Use this code
Intent intent = new Intent(first.this, Second.class);
Bundle extras = new Bundle();
extras.putString("value1",String.valueof(inp1.getText().toString()));
extras.putString("value2",String.valueof(inp2.getText().toString()));
intent.putExtras(extras);
startActivity(intent);
Then in your second Activity onCreate()
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String value1 = extras.getString("value1");
String value2 = extras.getString("value2");
To make thing easier and reusable you can make your own intent like this
public class MyIntent extent Intent{
private static final String FIRST_VALUE;
private static final String SECOND_VALUE;
public MyIntent(Context context, String firstValue, String secondValue){
super(context,MySecondActivity.class);
putExtra(FIRST_VALUE, firstValue);
putExtra(SECOND_VALUE, secondValue);
}
public String getFirstValue(){
getStringExtra(FIRST_VALUE);
}
public String getSecondValue(){
getStringExtra(SECOND_VALUE);
}
}
Sender:
startActivity(new MyIntent(this,"FirstString", "SecondString"));
Receiver Side:
MyIntent myIntent = (MyIntent)getIntent();
String firstValue = myIntent.getFirstValue();
String secondValue = myIntent.getSecondValue();

How to pass integer to a new activity in Android?

I'm new to android. I am taking a string and a numerical value as input from the MainActivity.
On pressing the button, the following method is called which invokes the second activity.
Am I passing the values correctly?
If yes, how do I receive both these values for use in the second activity and then print them ?
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
intent.putExtra("lol",message);
startActivity(intent);
}
P.S. I know Im not passing the integer at all. I dont know how to do that. Please help!
From the new Activity you should call getIntent(): https://developer.android.com/reference/android/app/Activity.html#getIntent()
Once you have the Intent you have to call getIntExtra(String, int): https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,%20int).
In your case, from the new Activity something like:
Intent intent = getIntent();
int lol = intent.getIntExtra("lol", 0);
Or shorter:
int lol = getIntent().getIntExtra("lol", 0);
That '0' is the default value for the int if the extra "lol" does not exist.
You just use this one for integer values
In second activity....
int i = getIntent().getIntExtra(SendingStringName, 0);
(or)
String progress = getIntent().getStringExtra(SendingStringName);
(or)
ArrayList< String> progress = getIntent().getStringArrayListExtra(name);
To send the data:
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
Bundle param = new Bundle();
param.putString("greeting",message);
param.putInteger("NumberInteger",i);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtras(param);
startActivity(intent);
To receive them:
In Class DisplayMessageActivity.java do:
Intent it = getIntent();
Bundle param = it.getExtras();
String capturedMessage= param.getString("greeting");
Integer captured_I = param.getInteger("NumberInteger");

Passing more than one array using bundle

I am trying to pass 2 double arrays from one activity to the other. However when I try to pass the values from the 2 arrays in the first activity to the arrays in the second activity I get just the values from the first array and its stored in both new arrays.
This is how I use the bundle to send the arrays
Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
intent.putExtras(bund);
Bundle bund2 = new Bundle();
bund2.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund2);
startActivity(intent);
And the on the receiving side I have:
Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
Bundle bund2 = intent.getExtras();
endLongitude = bund2.getDoubleArray(endLongitudeStr);
However the result is always just the values from the first array(in this case endLatitude)
What am i doing wrong?
Use same bundle object.
Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
bund.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund);
startActivity(intent);
Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
endLongitude = bund.getDoubleArray(endLongitudeStr);
If i recall correctly you can only use one bundle because if you make another bundle it will replace the previous bundle so what you need to do is put the bund1 and bun2 on the first bundle then retrieve it
use
Bundle bundle = new Bundle();
bundle = getIntent().getExtras();
String mystring=bundle.getString("bund1");
String mystring=bundle.getString("bund2");
Why do you use 2 Bundles? Just use one ...
Bundle bundle = new Bundle();
bundle.putDoubleArray(endLatitudeStr, endLatitude);
bundle.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtra(bundle);
startActivity(intent);
and ...
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
endLatitude = bundle.getDoubleArray(endLatitudeStr);
endLongitude = bundle.getDoubleArray(endLongitudeStr);

Put 2 extras in intent

I've made an intent and i've put inside 2 extras
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra(LABEL_NAME_DEFAULT, lblNameDefault);
intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault);
In my other activity i retrieve the info from them like this
//Get name from the label
Intent intent = getIntent();
String lblNameDefault = intent.getStringExtra(MainActivity.LABEL_NAME_DEFAULT);
String lbl2NameDefault = intent.getStringExtra(MainActivity.LABEL_2_NAME_DEFAULT);
//Set current name to editText
EditText labelNameDefault = (EditText)findViewById(R.id.set_name);
EditText label2NameDefault = (EditText)findViewById(R.id.set_name2);
labelNameDefault.setText(lblNameDefault, TextView.BufferType.EDITABLE);
label2NameDefault.setText(lbl2NameDefault, TextView.BufferType.EDITABLE);
The problem is that i receive to both labelNameDefault and label2NameDefault the result from LABEL_2_NAME_DEFAULT.
Can i only pass one extra?
How can i pass them both?
By default the value of labelName is "Counter 1" and the value of label2Name is "counter 2"
If i comment out intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault); the first label name is ok.
It looks like LABEL_2_NAME_DEFAULT is overwriting LABEL_NAME_DEFAULT
You could pass a 'bundle' of extras rather than individual extras if you like, for example:-
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("LABEL_NAME_DEFAULT",lblNameDefault);
extras.putString("LABEL_2_NAME_DEFAULT",lbl2NameDefault);
intent.putExtras(extras);
startActivity(intent);
Then in your Activity that your triggering, you can reference these like so:-
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Or (if you prefer):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Hope this helps! :-)
You can pass two (or more) extras in an intent. You need to make sure that LABEL_2_NAME_DEFAULT and LABEL_NAME_DEFAULT does not have the same value, though.
activity one
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra("LABEL_NAME_DEFAULT", lblNameDefault);
intent.putExtra("LABEL_2_NAME_DEFAULT", lbl2NameDefault);
activity two
username_string = getIntent().getExtras().getString("LABEL_NAME_DEFAULT");
password_string = getIntent().getExtras().getString("LABEL_2_NAME_DEFAULT");

Categories

Resources