I've got a Problem passing my Arraylist to the next Activity.
Here is my Error Code.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
By my understanding, i am trying to get an Arraylist, which does not exist.
I made sure the Arraylist is filled and my Intend values are correct, but im still getting the Error.
Creating the Arraylist
ArrayList<HashMap<String, String>> names = new ArrayList<HashMap<String, String>>();
Creating the intent
Intent intent = new Intent(First.this, Second.class);
intent.putExtra("names", names);
startActivity(intent);
Getting the intent
Intent intent = getIntent();
ArrayList<HashMap<String, String>> usernamen = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("names");
As said, the Arraylist get's filled correctly via
name = new HashMap<String, String>();
name.put("Name", spielername.getText().toString());
name.put("Gender", gender.getText().toString());
names.add(name);
If someone needs more information, i'll be happy to provide them.
Edit:
gobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(names.size() < 2){
Toast.makeText(Spieler.this, "No Informations", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Spieler.this, Games.class);
intent.putExtra("level", level.getProgress());
intent.putExtra("names", names);
startActivity(intent);
}
});
The Button is sending me to the second Activity, so the Array has to be filled.
ArrayList<HashMap<String,String>> names = new ArrayList<>();
HashMap<String,String> map = new HashMap<>();
map.put("Name", "Spieler");
map.put("Gender", "male");
names.add(map);
Send the data with Intent
Bundle bundle = new Bundle();
bundle.putSerializable("keyName",names);
Intent i=new Intent(v.getContext(), SecondActivity.class);
i.putExtras(bundle);
startActivity(i);
Get the data from Intent
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
Log.e("SecondActivity","yourArrayList--"+(ArrayList<HashMap<String,String>>)bundle.getSerializable("keyName"));
You need to serialize the value first, with either using Bundle:
Bundle bundle = new Bundle();
bundle.putSerializable("names", namen);
intent.putExtras(bundle);
or casting the list with Serializable:
intent.putExtra("names", (Serializable) names);
Related
I wanted to get the name of pizza on bill page put it isn't displaying(blank).
Menu.java:
bundle = new Bundle();
bundle.putString("name",pizzaName);
y = new Intent(this,Bill.class);
y.putExtras(bundle);
startActivity(y);
Bill.java:
text1=(TextView)findViewById(R.id.textView6);
y= new Intent();
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();
Output:
You do not need to do y= new Intent(); in Bill.java
Change code in Bill.java as follows. Also, for better naming convention, change Bill.java to BillActivity.java
TextView text1 = (TextView) findViewById(R.id.textView6);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String name = bundle.getString("name");
text1.setText(name);
}
You can simply pass and retrieve value like this.
// Creating and initializing an Intent object
Intent intent = new Intent(this, NextActivity.class);
// Attach the key value pair using putExtra to this intent
String pizza = "Margherita";
intent.putExtra("PIZZA_NAME", pizza);
// Start the activity
startActivity(intent);
// Get the current intent
Intent intent = getIntent();
// Get the attached extras from the intent
// We should use the same key as we used to attach the data.
String pizza = intent.getStringExtra("PIZZA_NAME");
Remove y= new Intent(); in your bill.class
No need to get y= new Intent(); when you get data from bundle
text1=(TextView)findViewById(R.id.textView6);
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();
I am begnning in android , I want to send String,Object,double,ArrayList from one activity to another . I already done with integer but when i want to send other data like object and ArrayList i am facing problem.
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Passing double data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found
Passing String data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");
or
Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");
Passing ArrayList data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");
Passing Object data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");
Note : Keep in mind your custom Class must implement the Serializable interface.
Passing HashMap data :
SenderActivity
HashMap<String, String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");
Passing Bitmap data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");
You can send object one activity to another by using Serializable class
check following example:
http://hmkcode.com/android-passing-java-object-another-activity/
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
you can send like:
Intent intent = new Intent(mcontext, activity.class);
intent .putExtra("Key",product);
startActivity(intent );
and get value like:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();`enter code here`
product = (ClassName) bundle.getSerializable("Key");
You can make a bean of the data you want to send like this
public class YourBean implements Serializable {
String yourString;
Object yourObject;
double yourDouble;
ArrayList<String> yourList;
public String getYourString() {
return yourString;
}
public void setYourString(String yourString) {
this.yourString = yourString;
}
public Object getYourObject() {
return yourObject;
}
public void setYourObject(Object yourObject) {
this.yourObject = yourObject;
}
public double getYourDouble() {
return yourDouble;
}
public void setYourDouble(double yourDouble) {
this.yourDouble = yourDouble;
}
public ArrayList<String> getYourList() {
return yourList;
}
public void setYourList(ArrayList<String> yourList) {
this.yourList = yourList;
}
}
then when you want to pass data in intent do like this
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
YourBean yourBean = new YourBean();
yourBean.setYourString("your string");
yourBean.setYourDouble(Your double);
yourBean.setYourObject(Your Object);
yourBean.setYourList(array list);
intent.putExtra("bean",yourBean);
startActivity(intent);
then you can get it like this in your SecondActivity like this
YourBean yourBean1 = (YourBean) getIntent().getSerializableExtra("bean");
You can use intents. In a intent you can put all sort of data, String, int, whatever you want.
In your case, in activity1, before going to activity2, you will store a String message this way :
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);
In activity2, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Then you can set the text in the TextView or where you want
Hope this helps you !
Intent mIntent = new Intent(MainActivity.this, AboutActivity.class);
ArrayList<String> mArray = new ArrayList<>();
mArray.add("Mon");
mArray.add("Tue");
mIntent.putStringArrayListExtra("Array", mArray);
startActivity(mIntent);
// In AboutActivity.java recieved data
ArrayList<String> mArray= getIntent().getExtras().getStringArrayList("Array");
Toast.makeText(AboutActivity.this, ""+mArray, Toast.LENGTH_LONG).show();
I am trying to pass the list of object from one activity to another. But not able to send it.
Here is my code.
setPrefBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent homePageIntent = new Intent(SetPreferences.this,
KlarityHome.class);// Connecting setPreferences page
// with Klarity Home Page
List<PracticeVO> tempList = new ArrayList<PracticeVO>();
tempList.add(practiceObj);
homePageIntent.putExtra("SelectedPractice", tempList.toArray());
startActivity(homePageIntent);
}
});
And retriving in 2nd activity like this :
Intent prefIntent = new Intent();
List preferencedPractices = (List) getIntent().getExtras().get("SelectedPractice");
ArrayAdapter<PracticeVO> praticeAdapter = new ArrayAdapter<PracticeVO>(this, android.R.layout.simple_spinner_item,
preferencedPractices);
praticeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
practiceSpin.setAdapter(praticeAdapter);
You need to use putExtra() function to pass values to another activity
Intent newIntent = new Intent(MainActivity.this, ResultActivity.class);
newIntent.putExtra("yourTxtField",yourTxtField.getText());
to receive you need to use other getIntent and Bundle.
Intent myIntent = getIntent();
Bundle valores = myIntent.getExtras();
//Objeto que vai calcular os resultados
Resultado resultado = new Resultado();
//Recupera os valores recebidos pelo intent
double seuDinheiro = Double.parseDouble((String) valores.get("seuDinheiro").toString());
To pass objects see this SO post
How do I pass an object from one activity to another on Android?
How to pass an object from one activity to another on Android
Try in this manner
ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);
In the Transfer.class
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");
What type is PracticeVO? In order to do what you are intending, PracticeVO needs to implement the Parcelable interface (http://developer.android.com/reference/android/os/Parcelable.html). Then you can use this version of the putExtra() method: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Parcelable[]).
Notification fundinProgram = (Notification) getIntent().getSerializableExtra(DETAIL_OBJECT);
implement Serializable in your PracticeVO class file
When sending from an activity:
List<PracticeVO> PracticeVO = new ArrayList<PracticeVO>;
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("PracticeVO", ArrayList<PracticeVO> PracticeVO);
startactivity(PracticeVO);
At the receiver side:
List<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
source: Link
I was able to achieve it this way:
Make your model class (PracticeVO) as well as your Activity class (KlarityHome) implement Serializable.
public class MyClass implements Serializable {...}
In your activity class, store your objects in an ArrayList rather than a List.
Finally, you can do this:
// create intent
Intent homePageIntent = new Intent(SetPreferences.this, KlarityHome.class);
// create your ArrayList and add your item(s)
List<PracticeVO> tempList = new ArrayList<PracticeVO>();
tempList.add(practiceObj);
// add your arraylist to your intent
homePageIntent.putExtra("SelectedPractice", tempList);
// go go go ;-)
startActivity(homePageIntent);
I hope this helps. Merry coding!
Right now, I know how to send an ArrayList of Strings through intent by doing this to send:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putStringArrayListExtra("list", list);
startActivity(intent);
and this to receive:
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("list");
But I want to send an ArrayList of LatLngs, like this:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putLatLngArrayListExtra("listLatLng", listLatLng);
startActivity(intent);
\\pseudo code, obviously
and then:
Intent i = getIntent();
ArrayList<String> list = i.getLatLngArrayListExtra("listLatLng");
\\pseudo code, obviously
Any ideas on how to do this? Is there any easy way or do I have to modify something? Thanks.
I would use android.location; It extends Parcelable and you can easily create and store the Locations in an arrayList created from lat and lon values
Intent i = new Intent(MainActivity.this, MapActivity.class);
i.putParcelableArrayListExtra(name, value);
i have tried passing ContentValues to insert to a database.
public long createEntry(String name, String description) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_DESCRIPTION, description);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
this method works. but now i wanna know how do i pass it to other forms through intent. i only know to use intent to transfer views/forms but not how to pass data.
public void onClick(View v) {
Log.i("lol","hello");
switch (v.getId()) {
case R.id.oil:
Intent i = new Intent("com.gtxradeon.brands.FirstBrandActivity");
startActivity(i);
finish();
break;
case R.id.android:
Intent i1 = new Intent(this, FirstBrandActivity.class);
startActivity(i1);
break;
default:
break;
}
and finally, what's the difference between Bundles and ContentValues.. i have tried reading on google tutorials for android but it got me more confused.
ContentValues are used to update/insert data into a permanent storage data structures like SQLite databases. It is important to use ContentValues to prevent SQL injections.
Bundles in the other hand are used to pass data among Activities using Intents. For example,
Bundle bundle = new Bundle();
bundle.putString("name", "John Doe");
Intent intent = new Intent();
intent.putExtras(bundle);
You can retrieve the Bundle in the next Activity by doing:
Bundle bundle = getIntent().getExtras();
String string = bundle.getString("name");
Another more common way of achieving the same result is:
Intent intent = new Intent();
intent.putExtra("name", "John Doe");
Then on the Activity you get the Intent by:
Intent receivedIntent = getIntent();
String name = receivedIntent.getStringExtra("name");
In general content values are used in database like Sql.
My suggestion to pass values from one activity to other in s ways.
1.Bundle.
2.Shared Preferences.
3.static variable.
Bundle:-
Bundle b=new Bundle();
b.putString("key","value you need to retrieve in another activity");
b.putString("name","nikhil");
Intent i=new Intent(ctx,Youractivityname.class);
i.putExtras(b);
StartActiviyt(i);
In your next activity page
Intent get=getIntent();
Bundle b=get.getExtras();
if(b!=null){
String name=b.getString("name");
}
SharedPreferences:-
SharedPreferences sp;
SharedPreferences.Editor edit;
sp = getSharedPreferences("enter", MODE_PRIVATE);
edit = sp.edit();
edit.putString("username", nikhil);
edit.commit();
In the next activity
SharedPreferences sp = getSharedPreferences("enter", MODE_PRIVATE);
user.setText(sp.getString("username", "default value"));
Static variable:-
In firstactivity:-
static String s="nikhil";
In next activity:-
String n=firstactivity.s