Я вызываю MapActivity с помощью startActivity(intent) после завершения асинхронной задачи. При этом я получаю базовое представление для моей MainActivity, а затем черный экран, когда запускается симуляция MapFragment.
У меня нет ошибки в logcat.
Я запускаю его на эмулированном пикселе 5X с использованием Android 9.
Я еще не коснулся макета деятельности. Что является причиной черного экрана?
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();
}
}
Я использую тот, который предоставляется через консоль API Google, API для карты Google работает, и ключ правильно отображается в google_maps_api.xml и AndroidManifest.xml.
Проблема возникает только в Android 9? Работает ли он правильно в другой версии Android???
Обычно черный экран вызван неправильными настройками ключа API.