1

I call a MapActivity with a startActivity(intent) after an async task ends. When doing so, I get the basic view for my MainActivity then a black screen when the MapFragment sims to be launching.

I've got no error in the logcat.

I'm running it on an emulated pixel 5X using android 9.

I haven't touched the layout of the activities yet. What is causing the black screen ?

MainActivity.java

public class MainActivity extends AppCompatActivity {

String json_string;

JSONObject json_object;

JSONArray json_array;

protected static JSONArray JSON_ARRAY;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent myIntent = new Intent(this, getJSON.class);
    startActivityForResult(myIntent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK){
        json_string = data.getStringExtra("JSON_string");
        Log.v("test 7",json_string);

        JSON_ARRAY = traitement(json_string);
        Intent myIntent2 = new Intent(this, MapsActivity.class);
        startActivity(myIntent2);
    }
}

public JSONArray traitement(String json){
    try {
        json_object = new JSONObject(json);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    try {

        json_array = json_object.getJSONArray("nightfall");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return json_array;
}

public static JSONArray getJSON_ARRAY() {
    return JSON_ARRAY;
}
}

MapsActivity.java

    private GoogleMap mMap;

JSONArray json_array;

Double lat, lng;

String nom;

LatLng lieu;

@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);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {

    json_array = MainActivity.getJSON_ARRAY();
    mMap = googleMap;

    int count = 0;
    try {
        while(count<json_array.length()) {
            JSONObject JO = json_array.getJSONObject(count);
            lat = JO.getDouble("lat");
            lng = JO.getDouble("lon");
            nom = JO.getString("nom");

            lieu = new LatLng(lat, lng);
            mMap.addMarker(new MarkerOptions().position(lieu).title(nom));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
MrRog85
  • 121
  • 1
  • 9

0 Answers0