ArrayList pass a null using Bundle - java

Currently I have an ArrayList that I wanted to pass to another activity. I've already applied getParcelableArrayListbut it pass null to another activity. I've check the array for null, it's not. In the current activity before I pass it at least.
This is my code:
RecyclerViewOffline.java
Toast.makeText(context,"Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(),OfflineActivity.class);
Log.d("Contexts Test : ", fileContents.get(holder.getLayoutPosition()).getTitle());
Bundle bundle = new Bundle();
//bundle.putParcelableArrayList("link",fileContents);
bundle.putParcelableArrayList("ArrayList",fileContents);
bundle.putInt("position",holder.getLayoutPosition());
intent.putExtra("bundle",bundle);
view.getContext().startActivity(intent);
My receiving end:
OfflineActivity.java
Bundle extras = getIntent().getBundleExtra("bundle");
if(extras != null) {
this.fileContents = extras.getParcelableArrayList("ArrayList");
layoutPosition = extras.getInt("position");
Log.d("Contexts Test Offline: ",""+ fileContents.get(layoutPosition).getTitle());
}
else{}

As I can see
You didnt add the arraylist in the Parcelable in and out method and contructor.
This will solve your issue.

Related

Passing string between Android Activities

I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;

Android - Pass Data Between Two Activities get NullPointerException

I have 3 activities we will call them A, B and C. A and B both have intents that send the view to C. And design on which one C came from different things happen on C's activity. I pass the data like this from A to C:
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
startActivity(intent);
Then in C's onCreate method I have something like this:
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Then I from B to C I have
Intent intent = new Intent(this, C.class);
startActivity(intent);
And then I get the NullPointerException, I am guessing this is because i have not specifed a String "Action" when going from B to C.
Now I could just add one line for B in this case but, if there were for more activities or a large project this would not be good, because for each activity going to one I would need this.
How can I have it so I don't get this exception without adding an "Action" sting to activity B?
Thanks for the help in advance.
EDIT
If I have this from A to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "A");
intent.putExtra("Action2", "A");
startActivity(intent);
And this from B to C
Intent intent = new Intent(this, C.class);
intent.putExtra("Action", "B");
startActivity(intent);
And then this in onCreate of C it fails when going from B to C:
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
else if (extras.getString("Action2").equals("A")) {
//DO Stuuf
}
}
Change the code in C class like this for checking the bundle is null or not.
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.getString("Action") != null)
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
}
if(extras.getString("Action2") != null)
if (extras.getString("Action2").equals("A2")) {
//DO SOMETHING
}
}
}
check below code :
if(getIntent().hasExtra("Action"))
//if you have passed "Action" from activity
else
//if you did not pass "Action" from activity
Try this...
Bundle extras = getIntent().getExtras();
if(extras != null){
if (extras.getString("Action").toString().equals("A") || extras.getString("Action").toString() == "A") {
//DO SOMETHING
}
}
Hope this solves
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getString("Action").equals("A")) {
//DO SOMETHING
}
you are always creating new Intent object when ever you are moving to new activity. Hence this issue. Try to copy required data in new intent and access, them.
NullPointerException This will Happen because When you are moving from Activity A to C you are passing intent with value and in activity C you are getting that value using Bundle. But When you Move From Activity B to C you are not passing values through intent and in Activity C you have Written this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
at this tme the extra is null So you will get Error.
DO like this
Bundle extras = getIntent().getExtras();
if(extras != null)//This one will check for null
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
EDIT
For passing Multiple values from activity
Intent intent= new Intent(this,NextActivity.class);
Bundle extra = new Bundle();
extra.putString("Action1","Value1");
extra.putString("Action2","Value2");
intent.putExtras(extra);
startActivity(intent);
In NextActivity
Intent i = getIntent();
Bundle extras = i.getExtras();
String strAction1 = extras.getString("Action1");
String strAction2 = extras.getString("Action2");
You are starting the Activity C twice( whenever you call startActivity(intent) ). First time from A and second time from B. Both times you are checking this
Bundle extras = getIntent().getExtras();
if (extras.getString("Action").equals("A")) {
//DO SOMETHING
}
Which is bound to give you a null pointer exception. Put up a break point in your IDE and verify it.
why don't you use some static variable to share the info between the activities in case you don't want to instantiate the class C more than once.

how to redirect from Normal java class to other java class which extends activity in android?

how to redirect from Normal java class to other java class which extends activity in android with putting some data.
to start a Another activity class
Intent i = new Intent(this, ActivityTwo.class);//this your current class
startActivity(i);
to pass a value in class 1
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
to get values .from ActivityTwo.class
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// do something with the data
}

Can startActivityForResult() send data?

I am learning android and curious to know as if startActivityForResult() contains the properties of startActivity() too, i.e. can it be used to send data like startActivity() beside receiving data from called activity?
Here is the code:
SendData Activity:
Intent data= new Intent(SendData.this, RecieveData.class);
Bundle check = new Bundle();
check.putString("UmerData", cheese);
medt.setText(cheese);
data.putExtras(check);
startActivityForResult(data, 5);
Should receive data in this activity (RecieveData Activity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recievedata);
Initializek();
Bundle got = getIntent().getExtras();
String ss= got.getString("UmerData");
if(getIntent()!=null && ss!=null ) {
rt1.setText("Is not Null");
}
}
Your help will be really appreciated !
Thanks
Yes, startActivity & startActivityForResult, both take intent as param. You can bundle launch data inside intent and pass it over to target activity.
When you use startActivityForResult(), you have to also create a onActivityResult() method within the Activity that called startActivityForResult(). onActivityResult() is where you can access the Intent stored by the Activity you start for a result.
In the Activity that is then started, you have to call setResult() to store the Intent that you store the data in.
Read over this: http://developer.android.com/reference/android/app/Activity.html#StartingActivities
edit: Misread your question. For passing a Bundle of options through, I would use this overload:
startActivity(Intent, int, Bundle)
I'm working in Xamarin Android so the code is C# but I had the same problem except I'm sending between separate apps. I eventually got it working so here is my example.
var intent = new Intent();
intent.SetComponent(new ComponentName("com.company.packageName", "com.company.packageName.activityName"));
intent.PutExtra(Intent.ExtraText, message);
StartActivityForResult(intent, 1);
// in app being started
protected override void OnResume()
{
base.OnResume();
Intent intent = Intent; // Equivalent to getIntent()
if (intent != null)
{
string str = intent.GetStringExtra(Intent.ExtraText);
if (str != null)
{
// Do stuff with str
}
else
{
//Show Error
}
}
else
{
//Show Error
}
}

Passing data from two activities to a third

I have total 3 activites. I pass the data from the first activity like this:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("day", mDay);
i.putExtra("month", mMonth+1);
i.putExtra("year", mYear);
startActivity(i);
get the data:
Bundle extras = getIntent().getExtras();
if(extras !=null) { .......
now I pass the data from the second activity:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("Hday", mDay);
i.putExtra("Hmonth", mMonth);
i.putExtra("Hyear", mYear);
startActivity(i);
and get the data from the second activity:
Bundle extras2 = getIntent().getExtras();
if(extras2 !=null) {
So, I have extras to get the data from the first activity, and extras2 to get the data from the second activity. But when I pass the data from any activity both extras are not null!
What have I done wrong?
The problem is that your third activity has no way of knowing which activity sent the bundle. You need to add a field that identifies what type of bundle this is so you can process accordingly. For instance, in activity 1:
Intent i = new Intent(getApplicationContext(), History.class);
i.putExtra("activity", (int)1);
i.putExtra("day", mDay);
i.putExtra("month", mMonth+1);
i.putExtra("year", mYear);
startActivity(i);
Then in 3rd activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int typeAct = extras.getInt("activity");
if (typeAct == 1) {
.......
pass the data in a one bundle
the first activity would for example have three values in the bundle then the second one retrieves the data from the bundle and addsor 'Append' to the second activity and finally in the third activity you can retrieve the data as a single bundle from the second activity.

Categories