Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取ArrayList坐标时出错_Java_Android_Arraylist - Fatal编程技术网

Java 获取ArrayList坐标时出错

Java 获取ArrayList坐标时出错,java,android,arraylist,Java,Android,Arraylist,我已经试着解决这个问题一周了,我已经试过这些问题,我已经做了一些测试,但是我仍然没有找到解决这个问题的方法 Java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“Java.lang.Object Java.util.ArrayList.get(int)” 它表示这一行: Latitude = Double.parseDouble (location.get (i) .get ("Latitude"). ToString ()); 完整代码: public

我已经试着解决这个问题一周了,我已经试过这些问题,我已经做了一些测试,但是我仍然没有找到解决这个问题的方法

Java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“Java.lang.Object Java.util.ArrayList.get(int)”

它表示这一行:

Latitude = Double.parseDouble (location.get (i) .get ("Latitude"). ToString ());
完整代码:

public class TabFragment2 extends Fragment {

    private IFragmentToActivity mCallback;
    MapView mMapView;
    private GoogleMap googleMap;


    // Latitude & Longitude
    private Double Latitude = 0.00;
    private Double Longitude = 0.00;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.tab_fragment_2, container,
                false);

        //*** Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        ArrayList<HashMap<String, String>> location = null;
        String url = "https://192.168.0.1/geo.php";
        try {

            JSONArray data = new JSONArray(getHttpGet(url));

            location = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("LocationID", c.getString("LocationID"));
                map.put("Latitude", c.getString("Latitude"));
                map.put("Longitude", c.getString("Longitude"));
                map.put("LocationName", c.getString("LocationName"));
                map.put("Tipo", c.getString("Tipo"));
                location.add(map);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        // *** Display Google Map
        googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView)).getMap();

        // *** Focus & Zoom
        Latitude = Double.parseDouble(location.get(0).get("Latitude").toString());
        Longitude = Double.parseDouble(location.get(0).get("Longitude").toString());
        LatLng coordinate = new LatLng(Latitude, Longitude);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));

        // *** Marker (Loop)
        for (int i = 0; i < location.size(); i++) {
            Latitude = Double.parseDouble(location.get(i).get("Latitude").toString());
            Longitude = Double.parseDouble(location.get(i).get("Longitude").toString());
            String name = location.get(i).get("LocationName").toString();
            String tipo = location.get(i).get("Tipo").toString();
            MarkerOptions marker = new MarkerOptions()
                    .position(new LatLng(Latitude, Longitude)).title(name);
            if(tipo.equals("CORTE")) {
                marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            }
            if(tipo.equals("RELIGACAO")) {
                marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
            }
            if(tipo.equals("VISTORIA")) {
                marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            }
            googleMap.addMarker(marker);
        }

        return v;
    }



    public static String getHttpGet(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Download OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (IFragmentToActivity) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement IFragmentToActivity");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    public void onRefresh() {
        Toast.makeText(getActivity(), "Fragment 2: Refresh called.",
                Toast.LENGTH_SHORT).show();
    }

    public void fragmentCommunication() {
        // mTextView1.setText("Hello from Tab Fragment 1");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

我猜如果try/catch语句捕捉到错误,那么可能是位置ArrayList实际上为null。所以,试着看看这是否会发生,以及如何避免它

ArrayList<HashMap<String, String>> location = null;
    String url = "https://192.168.0.1/geo.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));

        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("LocationID", c.getString("LocationID"));
            map.put("Latitude", c.getString("Latitude"));
            map.put("Longitude", c.getString("Longitude"));
            map.put("LocationName", c.getString("LocationName"));
            map.put("Tipo", c.getString("Tipo"));
            location.add(map);

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
ArrayList位置=null;
字符串url=”https://192.168.0.1/geo.php";
试一试{
JSONArray数据=新的JSONArray(getHttpGet(url));
位置=新的ArrayList();
HashMap图;
对于(int i=0;i
也许您应该在以下情况之前使用此条件:


如果(location.get(i)!=null)

据我所知,ArrayList内HashMap中的
.get(“Latitude”)
似乎返回null(键“Latitude”没有值),试图解析它就是引发异常的原因。尝试拆分这些语句并添加空检查。类似于:
if(location.get(i).get(“Latitude”)==null…
ArrayList<HashMap<String, String>> location = null;
    String url = "https://192.168.0.1/geo.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));

        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("LocationID", c.getString("LocationID"));
            map.put("Latitude", c.getString("Latitude"));
            map.put("Longitude", c.getString("Longitude"));
            map.put("LocationName", c.getString("LocationName"));
            map.put("Tipo", c.getString("Tipo"));
            location.add(map);

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }