MapFragment上的黑屏

时间:2019-03-16 15:28:40

标签: android google-maps android-activity 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();
    }
}

0 个答案:

没有答案
相关问题