Android 地图碎片上的黑屏
异步任务结束后,我使用startActivity(intent)调用MapActivity。当这样做时,我得到了MainActivity的基本视图,然后当MapFragment sims启动时会出现一个黑屏 我的日志没有错误 我使用android 9在模拟像素5X上运行它 我还没有触及活动的布局。是什么导致了黑屏 MainActivity.javaAndroid 地图碎片上的黑屏,android,google-maps,android-activity,mapfragment,Android,Google Maps,Android Activity,Mapfragment,异步任务结束后,我使用startActivity(intent)调用MapActivity。当这样做时,我得到了MainActivity的基本视图,然后当MapFragment sims启动时会出现一个黑屏 我的日志没有错误 我使用android 9在模拟像素5X上运行它 我还没有触及活动的布局。是什么导致了黑屏 MainActivity.java public class MainActivity extends AppCompatActivity { String json_string;
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;
}
}
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();
}
}
MapsActivity.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;
}
}
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();
}
}
私有谷歌地图mMap;
JSONArray json_数组;
双lat,液化天然气;
字符串名称;
拉特林;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment中。此方法仅在用户完成后触发
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
json_array=MainActivity.getJSON_array();
mMap=谷歌地图;
整数计数=0;
试一试{
虽然(计数通常黑屏是由不正确的API密钥设置引起的。我正在使用通过google API控制台提供的API,google map的API正在运行,并且密钥在google_maps_API.xml和AndroidManifest.xml中正确显示。这个问题只发生在Android 9中吗?它在其他Android版本中正常工作吗?通常黑屏是由不正确的API密钥设置导致我正在使用通过google API控制台提供的API,google map的API正在运行,并且密钥在google_maps_API.xml和AndroidManifest.xml中正确显示。该问题是否仅在Android 9中出现?它是否在其他Android版本中正常工作???