I'm trying to send an ArrayList<Pet>
from a Fragment
to MainActivity.java
and from here, if you press an ImageButton
(from the actionbar) it goes to another activity, in which I need the data from ArrayList. Well, I create an Intent
in my Fragment
class and a Bundle
to receive this data in my MainActivity
and I get an exception NullPointerException
when I receive this data.
Intent
code, from Fragment
class
//Intent
Intent intent = new Intent(getContext(), MainActivity.class);
String [] names = new String [pets.size()];
int []likesNumber = new int[pets.size()];
int [] photo = new int [pets.size()];
for (int i = 0;i<pets.size();i++){
names [i] = pets.get(i).getPetName();
likesNumber [i]=pets.get(i).getNumberOfLikes();
photo[i] = pets.get(i).getFoto();
}
intent.putExtra(getString(R.string.pet_name),names);
intent.putExtra(getString(R.string.numberOfLikes),likesNumber);
intent.putExtra(getString(R.string.photo),photo);
startActivity(intent);
Bundle
code to receive the data in MainActivity
class:
//Bundle para initialize pets
Bundle bundle = getIntent().getExtras();
String [] names = bundle.getStringArray(getString(R.string.pet_name));
int [] nLikes = bundle.getIntArray(getString(R.string.numberOfLikes));
int [] photo = bundle.getIntArray(getString(R.string.photo));
for (int i = 0; i<names.length;i++){
pets.add(new Pet(nLikes[i],names[i],photo[i]));
}
I get the exception in this line:
String [] names = bundle.getStringArray(getString(R.string.pet_name));
I don't know the reason why I get a NullPointerException
, because all the references are ok. If it's a logic error I wish you'd tell me too.
I don't know if it it's important but I commented that my Fragment
is composed of a RecyclerView
The error is as follows:
08-26 15:07:03.640 2551-2551/es.uclm.mylittlepets E/AndroidRuntime: FATAL EXCEPTION: main Process: es.uclm.mylittlepets, PID: 2551 java.lang.RuntimeException: Unable to start activity ComponentInfo{es.uclm.mylittlepets/es.uclm.mylittlepets.Layout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference at es.uclm.mylittlepets.Layout.MainActivity.onCreate(MainActivity.java:55) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The way I solve this error:
IN MY
Fragment
CLASS:
1.- I've created an interface FragmentIterationListener
that I'll use to communicate with the Activity
public interface FragmentIterationListener{
public void onFragmentIteration(Bundle parameters);
}
We create a private FragmentIterationListener mcallback
attribute which we are going to use to send data to Activity
.
2.- In onAttach(Activity activity)
method we'll do the following:
try {
mcallback = (FragmentIterationListener) activity;
}catch(ClassCastException ex){
Log.e("PetsFragment","The activity must implement the FragmentIterationListener interface");
}
Thus, we assign to our attribute the Activity
that is going to communicate.
3.- In the onCreateView()
method, besides inflating the Layout
, we instantiate the RecyclerView
.
recyclerView = (RecyclerView) view.findViewById(R.id.rvPets);
4.- In the onViewCreated()
method, we establish the RecyclerView
as a LinearLayout
, then we initialize the ArrayList <Pet> pets
and then we send it to MainActivity
class:
Bundle extras = new Bundle();
extras.putParcelableArrayList("EXTRA_PETS", pets);
mcallback.onFragmentIteration(extras);
IN MY
MainActivity.java
CLASS:
1.- We implement the interface that we created earlier.
public class MainActivity extends AppCompatActivity implements PetsFragment.FragmentIterationListener{
2.- We implement the required method of this interface:
public void onFragmentIteration(Bundle parameters) {
pets = parameters.getParcelableArrayList("EXTRA_PETS");
}
3.- We receive the data as follows: (in onCreate()
method):
Bundle bundle = new Bundle();
onFragmentIteration(bundle);