android - MapFragment 上的黑屏
问题描述
在异步任务结束后,我用 startActivity(intent) 调用 MapActivity。这样做时,我获得了 MainActivity 的基本视图,然后在 MapFragment 模拟启动时出现黑屏。
我在 logcat 中没有错误。
我正在使用 android 9 在模拟像素 5X 上运行它。
我还没有接触到活动的布局。是什么导致黑屏?
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();
}
}
解决方案
推荐阅读
- sql - 计算选定行之间的提前期 (SQL)
- javascript - 为 v-for 中的每个项目呈现一个异步的日期
- c - 在 C 中 - 我将如何使用 scanf 将随机数量的整数存储到二维数组中
- linux - GATT 库直接读取特征值,无需遍历服务
- c# - 在 C# 中单击绘制的形状
- android - setOnNavigationItemSelectedListener 似乎对我不起作用
- javascript - 如何通过 fetch 获取元素?
- css - 移动问题中的自动缩放
- javafx - JavaFX如何从Path类调整形状
- python - IPython Widgets - 处理许多按钮和文本框