What I'm trying to do is pass a list of locations to maps activity and put marker on those locations. I've tried using in MainActivity
public class MainActivity extends AppCompatActivity implements Parcelable {
ArrayList<Location> locs=new ArrayList<>();
...
locs.add(location);
...
Intent in = new Intent(context,MapsActivity.class);
in.putExtra("setlocations",locs);
startActivity(in);
...
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(locs);
}
}
And then in MapsActivity's onCreate()
Bundle extras = getIntent().getExtras();
if(extras != null){
locs=(ArrayList<Location>)getIntent().getParcelableExtra("setlocations");
addMarkeratLocation();
}
The addMarkeratLocation() method uses locs list for adding markers using for loop
public void addMarkeratLocation(){
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.mipmap.dott);
LatLng addpoint=new LatLng(0.0,0.0);
for(int i=0;i<locs.size();i++){
addpoint = new LatLng(locs.get(i).getLatitude(), locs.get(i).getLongitude());
mMap.addMarker(new MarkerOptions().position(addpoint).icon(icon));
}
mMap.moveCamera(CameraUpdateFactory.newLatLng(addpoint));
}
My application crashes when the intent is fired. And the logcat shows
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
Why it is showing null? This is first time I'm using Parcelable interface.. Is there anything that I'm missing? Any input will be appreciated, thanks.