-3

I'm trying to display a new activity from a MapActivity. In fact, I want to open a new activity by clicking on the InfoWindow of the Marker who are on my Map

I tried to use intent in an OnInfoWindowClick Method but the application is still crashing when I click on an InfoWindow

This is my MapActivity :

public class MapsActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback, GoogleMap.OnMapClickListener {

private GoogleMap mMap;
private Marker mMarker;
int i = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(mMap.MAP_TYPE_NORMAL); // Here is where you set the map type
    // Add a marker in Sydney and move the camera
    LatLng dfltMarkLyon = new LatLng(45.760102,4.839177);
    mMarker.setTag(0);
    mMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(45.756363,4.833219)).title("L'Institut Restaurant").snippet("Restaurant").icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    mMarker.setTag(1);

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(MapsActivity.this, "Test " + i, Toast.LENGTH_SHORT).show();
            i += 1;
            Intent intent = new Intent(MapsActivity.this, PlaceActivity.class);
            startActivity(intent);
        }
    });

    //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    float zoomLevel = 13.0f; //This goes up to 21
    mMap.setOnMapClickListener(this);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dfltMarkLyon, zoomLevel));
}

And this is the class where is situated the activity I want to open :

   public class PlaceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        //Toast.makeText(PlaceActivity.this, "New Class", Toast.LENGTH_SHORT).show();setContentView(R.layout.activity_description);

    }


    }

When I click on the InfoWindow my application crash

What can I do to improve that?

Zoe
  • 27,060
  • 21
  • 118
  • 148
WelGalaxy
  • 27
  • 1
  • 1
  • 10
  • The statement `mMarker.setTag(0);` is done before you add the marker so this would result in a null pointer exception. Remove that since you do it again after the add marker. Add logcat to help see specifically where the crash is occurring but from inspection of your post that is a error. –  Jun 16 '19 at 00:36
  • 1
    what is your error code – ismail alaoui Jun 16 '19 at 00:43
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Jun 16 '19 at 13:41

1 Answers1

1

Your code, which is starting new activity, is correct. You can use both.

Intent intent = new Intent(MapsActivity.this, PlaceActivity.class);
Intent intent = new Intent(getContext(), PlaceActivity.class);

The problem seems to be in PlaceActivity. please check you added this activity on AndroidManifest.xml file. and check you have activity_description.xml file in resource.

It's the problem depending on your project setting and structure. You can check it by changing the start activity as PlaceActivity in AndroidManifest.xml.

SuperStar518
  • 2,814
  • 2
  • 20
  • 35
Wencheng Li
  • 126
  • 6