即使将数据推入其中,ArrayList也显示为空

时间:2017-08-08 05:14:09

标签: android asynchronous arraylist null

我在程序中有全局初始化的Arraylist,并在JSON数据中添加了纬度和经度。在 makeJsonPopularDestinationDetailPage()中,该值被添加到ArrayList中并且它非常好,正如我预期的那样函数。但是当我尝试访问另一个函数时,它显示为null。为什么arraylist不显示null?

PopularDestinationGridDetail

    private void makeJsonPopularDestinationDetailPage() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String URL1 = Navigation_URL_Popular_Destination + "/1";

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {


                            //
                            JSONObject jsonObject = new JSONObject(response);
                            title = jsonObject.getString("title");
                            description = jsonObject.getString("description");
                            latitude = jsonObject.getString("latitude");
                            longitude = jsonObject.getString("longitude");
                            templeTitle.setText(title);
                            textDescription.setText(description);

                            destination_list_google_map.add(new ClassDestinationGoogleMap(latitude, longitude));
                            addMarker();


                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

//
//                        System.out.println("Wow" + destination_list_google_map.get(0).getLatitude());
//                        System.out.println("Wow" + destination_list_google_map.get(0).getLongitude());




                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.toString(), LENGTH_LONG).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                //  headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }

        };


        requestQueue.add(stringRequest);

    }





    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.destination_button_gallery) {
            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_changer_detaildestination, new FragmentImageGallery())
                    .commit();


            imageButtongallery.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_withclick));
            imageButtongallery.setImageResource(R.mipmap.destination_imagebutton_gallery_onclick);

            ButtonVideo.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            ButtonVideo.setImageResource(R.mipmap.destination_video_button_without_click);


            ButtonAudio.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            ButtonAudio.setImageResource(R.mipmap.destination_audio_button_without_click);


        } else if (id == R.id.destination_button_video) {
            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_changer_detaildestination, new FragmentVideoDestination()).commit();


            imageButtongallery.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            imageButtongallery.setImageResource(R.mipmap.destination_imagebutton_gallery_without_click);

            ButtonVideo.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_withclick));
            ButtonVideo.setImageResource(R.mipmap.destination_video_button_onclick);


            ButtonAudio.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            ButtonAudio.setImageResource(R.mipmap.destination_audio_button_without_click);


        } else if (id == R.id.destination_button_audio) {

            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_changer_detaildestination, new FragmentAudioDestination()).commit();

            imageButtongallery.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            imageButtongallery.setImageResource(R.mipmap.destination_imagebutton_gallery_without_click);

            ButtonVideo.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_without_click));
            ButtonVideo.setImageResource(R.mipmap.destination_video_button_without_click);


            ButtonAudio.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.destination_circular_shape_icon_withclick));
            ButtonAudio.setImageResource(R.mipmap.destination_audio_button_onclick);


        } else {

            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_changer_detaildestination, new FragmentImageGallery())
                    .commit();

        }


    }


    void addMarker(){



        Double x = Double.valueOf(destination_list_google_map.get(0).getLatitude());
        Double y = Double.valueOf(destination_list_google_map.get(0).getLongitude());


        System.out.println("x"+x);
        System.out.println("y"+y);


        LatLng populardestination = new LatLng(x, y);
        mMap.addMarker(new MarkerOptions().position(populardestination).title("Suman"));
        CameraPosition cameraPosition = new CameraPosition.Builder().target(populardestination).zoom(15).build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));





    }




    @Override
    public void onMapReady(GoogleMap googleMap) {

        // System.out.println("object" + destination_list_google_map.get(position).getLatitude());

//        System.out.println("Wow" + destination_list_google_map.get(0).getLatitude());  *****why null, idon't get it
//        System.out.println("Wow" + destination_list_google_map.get(0).getLongitude());

//
        // destination_list_google_map.get(0).getLatitude();

//
//        Double x = Double.valueOf(27.6644);
//        Double y = Double.valueOf(85.3188);
//        Double x = Double.valueOf(destination_list_google_map.get(0).getLatitude());
//        Double y = Double.valueOf(destination_list_google_map.get(0).getLongitude());

        mMap = googleMap;


//        LatLng populardestination = new LatLng(x, y);
//        googleMap.addMarker(new MarkerOptions().position(populardestination).title("Suman"));
//        CameraPosition cameraPosition = new CameraPosition.Builder().target(populardestination).zoom(15).build();
//        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }

ClassDestinationGoogleMap

public class ClassDestinationGoogleMap implements Serializable {

    public String latitude;
    public String longitude;

    public ClassDestinationGoogleMap(String lati, String longi) {
        latitude = lati;
        longitude = longi;


    }


    public String getLatitude() {
        return latitude;
    }

    public String getLongitude() {
        return longitude;
    }
}
  

我没有确切的理由,为什么它显示为null。任何人都可以   解释一下?

1 个答案:

答案 0 :(得分:1)

根据您发布的代码,这似乎是一个时间问题。

即。 arrayList正在填充HTTP请求的完成块中。而且,您正试图在onMapReady事件期间访问arraylist中的值。

这里的问题是,当调用onMapReady时,很可能请求尚未完成,并且您的数组仍然为null。因此,您需要在访问arrayList之前确保请求已完成。

例如,如果要向地图添加标记,而不是在onMapReady中添加标记,则可以创建另一个将在请求完成块中调用的函数:

public void onResponse(String response) {
  ...
  destination_list_google_map.add(new ClassDestinationGoogleMap(latitude, longitude));
  ...
  addMarkers();
}

...
...

@Override
public void onMapReady(GoogleMap googleMap) {
  //save map object to variable
  mMap = googleMap;
}

void addMarkers() {
  //ArrayList will have values here
  Double x = Double.valueOf(destination_list_google_map.get(0).getLatitude());
  Double y = Double.valueOf(destination_list_google_map.get(0).getLongitude());

  LatLng populardestination = new LatLng(x, y);
  mMap.addMarker(new MarkerOptions().position(populardestination).title("Suman"));
}