I am trying to pass longitude and latitude variables into another activity for the Google maps fragment. So far I have managed to do this
Intent i = new Intent(Main.this, Main2.class);
i.putExtra(Double.toString(gettextLong), xLocation.getLongitude());
i.putExtra(Double.toString(gettextLat), xLocation.getLatitude());'
How do I receive it in the other activity?
Give relevant key like
Intent i = new Intent(Main.this, Main2.class);
i.putExtra("longitude", xLocation.getLongitude());
i.putExtra("latitude", xLocation.getLatitude());
and in Main2 Activity
Bundle extras = getIntent().getExtras();
if(extras !=null && extras.getDouble("latitude") != null && extras.getDouble("longitude") != null) {
double lat = extras.getDouble("latitude");
double lng = extras.getDouble("longitude");
}
Try:
Intent i = new Intent(Main.this, Main2.class);
i.putExtra("lon", xLocation.getLongitude());
i.putExtra("lat", xLocation.getLatitude());
Like this:
int lat = getIntent().getExtras().getDouble("lat");
int lon = getIntent().getExtras().getDouble("lon");
Use getIntent() and getDoubleExtra() in yours second activity. But pass it in different way. First argument in putExtra should be some String but you have to know him, it is needed to use later in second activity. Like:
public static final String FIRST = "firstArgument";
i.putExtra(FIRST, xLocation.getLongitude());
And in Main2:
double i = getIntent().getDoubleExtra(Main.FIRST, 0);
Related
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);
}
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.
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);
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+"");
just change
bb.get("Int");
to
bb.getInt("Int");
I have 2 activities. Activity A sends a Number to activity B and activity recieves and uses the Number. The problem is that activity B produces FormatExeption errors.
Activty A code:
EditText set_limit = findViewById(R.id.editText2);
Bundle set_limit_basic = new Bundle();
set_limit_basic.putString("limit_basic", String.valueOf(set_limit));
Intent Aintent = new Intent(A.this, B.class);
Aintent.putExtras(set_limit_basic);
startActivity(Aintent);
Activity B code:
Bundle set_limit_basic = getIntent().getExtras();
if (set_limit_basic != null) {
String B_string = set_limit_basic.getString("limit_basic");
if ( B_string .trim().length() == 0){
limit_number = Integer.parseInt(B_string);
Several points:
You shouldn't be converting set_limit to a string; set_limit is an EditText widget. Instead, you should be putting the contents of the view (the text it is displaying).
There's no reason to explicitly construct your own extras bundle. Just use one of the putExtra methods defined in the Intent class.
The error checking is probably better handled in activity A instead of activity B.
You seem to have a logic error in activity B, in that you are only attempting to parse the limit number when the trimmed text is empty. That seems backwards.
Putting all this together, I'd rewrite your code as follows:
Activity A:
EditText set_limit = findViewById(R.id.editText2);
CharSequence text = set_limit.getText();
if (TextUtils.isEmpty(text)) {
// handle case of no text
} else {
try {
int limit_number = Integer.parseInt(text.toString());
Intent intent = new Intent(A.this, B.class);
intent.putExtra("limit_basic", limit_number);
startActivity(intent);
} catch (NumberFormatException e) {
// handle case of improperly formatted text
}
}
Activity B:
limit_number = getIntExtra("limit_basic", -1 /* or other default value */);
// or, if you want to explicitly check for presence of the extra:
if (hasExtra("limit_basic")) {
limit_number = getIntExtra("limit_basic", -1);
}
In 1st Activity you are trying to use String.valueOf(edittext);
( set_limit is a variable of type EditText so the string value will be #djfjckckc78 something like this...which is surely not a number)
try
String.valueOf(set_limit.getText().toString());
i.e,
set_limit_basic.putString("limit_basic",String.valueOf(set_limit.getText().toString()));
and also in Activity B,
if ( B_string .trim().length() > 0){
}
If you try to convert String.valueOf(variable) to a number it will throw a NumberFormatException at runtime because that string aint a number !!
Ok, so I am trying to get two different String arrays from one activity, through an alarmManager to a BroadcastReceiver (called AlarmReceiver), and so far have been using myIntentName.putExtra() and the relevant .getExtra() on the broadcastReceiver side of things. Here's my relevant code:
In the first activity (this is called and the code gets to the AlarmReceiver):
private void setAlarmManager() {
Intent intent = new Intent(this, AlarmReceiver.class);
//I have already defined askArray and answerArray (they aren't null, but are dynamic
//and exactly how I define them is quite complex so not included here)
intent.putExtra("askArray", askArray);
intent.putExtra("answerArray", answerArray);
PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 61000, sender);
}
At the top of the AlarmReceiver(which extends BroadcastReceiver):
String[] askArray = new String[2];
String[] answerArray = new String[2];
In the broadcastReceiver, in the onReceive():
askArray = intent.getExtras().getStringArray("askArray");
answerArray = intent.getExtras().getStringArray("answerArray");
for (int i = 0; i < askArray.length; i++){ //I get a NullPointerException on this line
Log.i("L3R", "AskArray["+i+"]: " + askArray[i]);
}
use this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ContactItem contactData = (ContactItem) listView.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), ContactDetail.class);
intent.putExtra("DATA_KEY", contactData);
startActivity(intent);
}
});
for details
Intent i = getIntent();
Bundle b = i.getExtras();
// getting attached intent data
ContactItem contact = (ContactItem) b.getSerializable("DATA_KEY");
// displaying selected contact name
txtName.setText(contact.getName());
txtPhone.setText(contact.getPhone());
txtMobile.setText(contact.getMobile());
txtEmail.setText(contact.getEmail());
you need to get array from intent using following code
askArray = intent.getStringArrayExtra("askArray");
answerArray = intent.getStringArrayExtra("answerArray");
You're presumably getting a NullPointerException when accessing askArray.length, which means it's a good idea to check if askArray == null before attempting to iterate through the array. This will prevent your app from crashing if an error occurs passing these values to the BroadcastReceiver.
It's also common practice to check if getExtras() returned anything useful, like so:
Bundle extras = intent.getExtras();
if(extras != null) {
askArray = extras.getStringArray("askArray");
answerArray = extras.getStringArray("answerArray");
if(askArray != null) {
for (int i = 0; i < askArray.length; i++) {
Log.i("L3R", "AskArray["+i+"]: " + askArray[i]);
}
}
} else {
Log.e("L3R", "Failed to getExtras()");
// deal with missing values here
}
If this logs the failure message every time, then the problem might be related to how you're setting the intent variable. It appears you're setting it correctly before "putting" the arrays, but it's not apparent how you're setting it when "getting" the arrays. This should be your code in the BroadcastReceiver:
Intent intent = getIntent();
Ok, sorry but it turns out that the problem wasn't with the part of the code I posted. I was using an alarm manager that had a quirk I wasn't aware of, but thanks for all the answers, here is the link: Android cannot pass intent extras though AlarmManager
And here is the relevant information(copied from the link):
Haresh's code is not the complete answer... I used a Bundle and I tried without Bundle but I got null's either way when I attempting to obtain the strings from the extra's !!
The exact problem, in your code, is with the PendingIntent !
This is wrong if you're trying to pass extra's :
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);
Because the 0 for the flags is what will cause you a headache
This is the right way to do it - specify a flag !
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This is probably such a popular problem because Google's sample code neglected to include Extra's in an Alarm.
Maybe this solve your problem:
At the top of the broadcast-receiver change fields to this:
String[] askArray;
String[] answerArray;
That's all. Good luck.
I want to send a value to an activity that HighScore which shows the highest score of the game.
I am saving the score at the game activity in shared preferences, and I am trying to send the score(string value) to the activity HighScore.
Intent i = new Intent(this, HighScoreActivity.class);
i.putExtra("classicHighScore", highScore);
it seems like you are sending your string in an intent.
which means, in your HighScore activity, write
String highScore = getIntent.getStringExtra("classicHighScore", "");
and there you go,
String highScore now has your value,
you can log it to check :
Log.d("HIGH SCORE VALUE ", highScore);
You need to save data in a SharedPreferences object in the first Activity and read it in the other activity.
Saving the score using SharedPreferences in the first Activity:
SharedPreferences preferences = getSharedPreferences("SharedScore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("classicHighScore", highScore);
editor.commit();
Reading the score from SharedPreference in the other activity:
SharedPreferences preferences = getSharedPreferences("SharedScore", MODE_PRIVATE);
int score = preferences.getInt("classicHighScore", -1);
If you want send a String between activities you must create a Bundle.
Intent i = new Intent(this, HighScoreActivity.class);
Bundle bundle = new Bundle();
bundle.putString("classicHighScore", highScore);
i.putExtra(bundle);
To read de parameter in HighScoreActivity
Bundle arguments = getIntent().getExtras();
String classicHighScore = arguments.getString("classicHighScore");
I am trying to convert a value from Double to String in an Android Activity.I can get this to work with my first example here below (working in the sense of no squigly error from Eclipse). However I am curious as to why the second example is not working.
First example
balance = (TextView) findViewById(R.id.textViewCardBalance);
Intent intent = getIntent();
if (intent.getExtras() != null) {
balance.setText(String.valueOf((long)intent.getDoubleExtra("balance", 0.00)));
}
Second example below not working (Error: "Cannot cast from Double to Long"
balance = (TextView) findViewById(R.id.textViewCardBalance);
Double cardBalance;
Intent intent = getIntent();
if (intent.getExtras() != null) {
cardBalance = intent.getDoubleExtra("balance", 0.00);
balance.setText(String.valueOf((long)cardBalance);
}
Would anyone know how I can get the second example to work as I need to log the value retrieved from the intent before passing it to the TextView.
Thanks
Why can't you do this?
balance.setText(cardBalance + "");
String yourDoubleString = String.valueOf(yourDouble);
in your case:
String yourDoubleString = String.valueOf(intent.getDoubleExtra("balance", 0.00));
using String v = ""+String.valueOf((long)cardBalance) not work?