Java extras bundle is returning null values - java

My bundle is returning null strings for the contained extras. Not NPEs, actual "null" values. Any ideas on why this would be happening?
new bundle
String u = null;
Bundle b = new Bundle();
Intent i = new Intent(view.getContext(), ******.class);
u = api.companyData.link.get(position);
Log.d("URL++++++++++++++++++++", u);
b.putString("graphic", api.companyData.graphic);
b.putString("name", api.companyData.name);
b.putString("url", u);
i.putExtras(b);
startActivity(i);
The log statement is returning the url fine.
Receiver of bundle
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Log.d("EXTRAS", extras.getString("name")+extras.getString("graphic")+extras.getString("link"));
D/EXTRAS ( 4698): nullnullnull

I always do it this way:
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("url", u);
and then
String url = getIntent().getStringExtra("url");
I've never tried it your way, but if you look at the Android docs for putExtras(Bundle), it says:
Add a set of extended data to the intent. The keys must include a
package prefix, for example the app com.android.contacts would use
names like "com.android.contacts.ShowAll".
That you're not doing that may be the reason for the failure.

put the objects in the intent directly, e.g.
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("graphic", ...);
i.putExtra("name", ...);
i.putExtra("url", ....);
Then in the receiver activity:
getIntent().getStringExtra("graphic");
getIntent().getStringExtra("name");
getIntent().getStringExtra("url");

Related

Send multiple values between two activities

Hi I´m just starting to learn how to use Android Studio. And I want to try to send the values of the choices the user makes from one activity to the other.
You can pass data like this(this is from SecondActivity):
Intent intent = new Intent(this, ThirdActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
and than in ThirdActivity in onCreate method:
String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
Also, best practise is to have key "EXTRA_SESSION_ID" stored in public static variable so you have only one object
1.Change this to arraylist
String[] mackor_names = {
"Tonfisk Macka 30:-",
"Skagen Macka 35:-",
"Kyckling Macka 35:-",
"Curryröra Macka 30:-",
"Ost o Kalkon Macka 25:-",
"Köttbulle Macka 25:-",
"Falafel Macka 20:-"
};
ArrayList<String> list = new ArrayList<>();
list.add("Tonfisk Macka 30:-");
list.add("Skagen Macka 35:-"); ......
2.and then use
intent.putStringArrayListExtra("test",list);
3.and to get values
Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");
//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
Insead of array you can use bundle

Is there a point in using Bundle class in android for passing info between activities?

This is my main activity:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String name = "James";
intent.putExtra("title", name);
MainActivity.this.startActivity(intent);
and this is the second activity:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("title");
tv.setText(name);
I know that I can take out the Bundle class and just get the string by intent object. So, in this case there is no need for Bundle class. So what is this class good for in this case? It seems to me it's just a redundant line of code.
In what cases using Bundle is advised for passing variables between classes?

GetStringExtra always return null

I tried to send string from onclick recyclerview to the activity, all doing well except one of this.
GeneralItem generalItem = (GeneralItem) consolidatedList.get(position);
Intent intent = new Intent(getActivity(), DetailPengumuman.class);
intent.putExtra("getnama", generalItem.getDaftarPengumuman().getNama_p().toString());
Log.e("untaging","ada isinya : "+generalItem.getDaftarPengumuman().getNama_p().toString());
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi());
startActivity(intent);
I also tried to log getnama in untaging tag its doing well and return me the data in log. But when I retrieve it in another activity, It always return null.
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
You can first check for if intent contains data or not..
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("Key")) {
String value = bundle.getString("Key");
}
}
Trying Adding .toString() to call getStringExtra :
intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng().toString());
intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul().toString());
intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi().toString);
Solved thanks,
This because of I missing the String attribute
Intent intent = getIntent();
tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
judulPengumumanGet = intent.getStringExtra("judulpengumuman");
namaPengumumanGet = intent.getStringExtra("getnama");
deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");
Log.e("untaging","nama : " +namaMatkulGet);
I receiving extra in namaPengumumanGet and I log another String which is namaMatkkulGet

Android: Passing String Array over Multiple Activities

I am trying to pass a single string array through about 3 classes to finally have the contents of the array[1] printed to a textview. I've been using intent to achieve this with my arraylists and it works fine. For some reason I'm unable to get it working with a measly string array. Here's what I'm doing.
Origin Activity of String Array:
private String [] decisionInput = new String[1];
textData = etShouldI.getText().toString();
if (!textData.matches("")){
decisionInput[0] = (String.valueOf(textData));
test.setText(decisionInput[0]); //TEST WORKS
//CREATE BUNDLE
Bundle bundle = new Bundle();
bundle.putStringArray("decision", decisionInput);
//SEND BUNDLE DATA
Intent intent = new Intent(this,Pro.class);
intent.putExtras(bundle);
startActivity(intent);}
In my next Activity I've got the following, in order to receive the data, and send it off to the next Activity, and so on...
String[] dPasser = new String[1];
#ONCREATE
//BUNDLE RECEIVER
Bundle bundle = getIntent().getExtras();
dPasser = bundle.getStringArray("decision");
thisText.setText(String.valueOf(dPasser)); //TV currently returns null...
#ONCLICK
//SEND DECISION DATA TO NEXT ACTIVITY
Intent intent = new Intent(this, Next.class);
Bundle b = new Bundle();
b.putStringArray("decision", dPasser);
intent.putExtras(b);
startActivity(intent);
What the $%#& am I doing wrong guys?
You put the code below in a file named data, in your code you then use just it by calling data.array
public class data {
public String[] array = new String[1];
}
But going with just passing through a String[] you shouldn't need bundle.
simply
intent.putExtra("stringArray".String[]);
and get it with
this.getIntent().getStringArrayExtra("stringArray")

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);

Categories

Resources