1

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.

3 Answers 3

5
`Location` class is not a Parcalable class.

Intent.putExtra() function only accept primitive and Parcelable data type. So you can not pass List<Location> through intent. Instead you can use Gson library to serialise you arraylist of Location and pass it as Json string to MapActivity and In MapActivity deserialise Json string to array of Location.

Sign up to request clarification or add additional context in comments.

2 Comments

can you provide any example of that..?
Sure I'll put details answer
4

Step 1: First Import Gson Library using Gradle Script

Add this line in your Gradle dependency

dependencies {
    compile 'com.google.code.gson:gson:2.7'
}

Step 2: Convert List into JSON String and pass the JSON String from MainActivity to MapsActivity

        Location location1 = new Location("");
        location1.setLatitude(12.124);
        location1.setLongitude(77.124);
        Location location2 = new Location("");
        location2.setLatitude(12.765);
        location2.setLatitude(77.8965);

        List<Location> locations = new ArrayList<Location>();
        locations.add(location1);
        locations.add(location2);

        Gson gson = new Gson();
        String jsonString = gson.toJson(locations);
        Intent intent = new Intent(MainActivity.this,MapsActivity.class);
        intent.putExtra("KEY_LOCATIONS",jsonString);
        startActivity(intent);

Step 3: Get the JSON String from Bundle and convert it to List

        Bundle bundle = getIntent().getExtras();
        String jsonString = bundle.getString("KEY_LOCATIONS");

        Gson gson = new Gson();
        Type listOfLocationType = new TypeToken<List<Location>>() {}.getType();
        List<Location> locations = gson.fromJson(jsonString,listOfLocationType );

I hope this might help you!!

7 Comments

There are multiple import options for Type package.. which one shall I import?
import java.lang.reflect.Type;
App crashes giving error java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference on the line of adding marker, this is how I'm adding marker mMap.addMarker(new MarkerOptions().position(addpoint).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
The variables location1 and location2 are incomplete that may be the issue but I'm not sure. Try to pass complete location variable. But above method will sure let you pass List<Location> to other Activity
I'm adding locations to the list in onLocationChanged method
|
0

ArrayList locs=new ArrayList<>(); Location class should be implemented with Parcelable, you already implemented Parcelable but it is for activity that should be for Location class.

Please refer below link for more info related your query.

Passing arraylist of custom object to another activity

2 Comments

That Location class is the android's inbuilt class... how can I implement that with Parcelable??
you need to create your own class and that class should have that location class example:- create another class MyLocationClass so array will be ArrayList<MyLocationClass> and MyLocationClass should have implemented with Parcelable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.