这是我的getCurrentLocation方法:

private CurrentLocation getCurrentLocation(String jsonData) throws JSONException {
    JSONObject object = new JSONObject(jsonData);
    JSONArray results = object.getJSONArray("results");

    JSONObject objects = results.getJSONObject(0);
    JSONObject geometry = objects.getJSONObject("geometry");
    JSONObject location = geometry.getJSONObject("location");
    //instance of CurrentLocation class
    CurrentLocation currentLocation = new CurrentLocation();
    currentLocation.setLongitude(location.getDouble("lat"));
    currentLocation.setLatitude(location.getDouble("lng"));
    Log.i(TAG, "FROM JSON: " + currentLocation.getLatitude());
    Log.i(TAG, "FROM JSON: " + currentLocation.getLongitude());
return currentLocation;
}


从JSON打印出来:-117.9145036
来自JSON:33.8352932,这是正确的,

我在行上收到NullPointerException错误:

 private void updateLocation() {
       getForecast(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}


我知道私有void getForecast(双纬度,双经度)可以工作,因为我已经将两个double用作参数,并且可以工作。
我对android还是很陌生,我知道工作室本身有一个geolocator,但是我想通过API来实现。我不确定nullException指向什么,任何帮助将不胜感激。谢谢。

如果有帮助,这是我的其他方法:
Oncreate:

This is my Oncreate method:

public void onClick(View view) {
             String name = mNameField.getText().toString();
             getLocation(name);
             updateLocation(); // error here?


和getLocation:

public void getLocation(String name) {
    //get Google api
    String geoLocatingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address="+name+"&key=AIzaSyAsJJIEeB952KpjpoFoMuYHd6gev85uzYs";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(geoLocatingUrl)
                .build();

        Call call = client.newCall(request);
        //UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

        @Override
        public void onResponse(Response response) throws IOException {

            try {
                   String jsonData = response.body().string();
                if (response.isSuccessful()) {

                    mCurrentLocation = getCurrentLocation(jsonData);
                    Log.v(TAG, jsonData);

                } else {
                    alertUserAboutError();

                }
            } catch (IOException e) {
                Log.e(TAG, "Exception caught: ", e);

            }
            catch (JSONException e) {
            }
        }
    });
}


和getForecast:

private void getForecast(double latitude, double longitude) {
    String apiKey = "//myapikey";

    String forecastUrl =  "https://api.forecast.io/forecast/" + apiKey +
            "/" + latitude + "," + longitude;

    if (isNetworkAvailable()) { //runs if network is there and available
        toggleRefresh();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(forecastUrl)
                .build();

        Call call = client.newCall(request);
        //UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();
                    }
                });
                alertUserAboutError();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();
                    }
                });
                try {
                    String jsonData = response.body().string();
                    if (response.isSuccessful()) {

                        mCurrentWeather = getCurrentDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateDisplay();
                            }
                        });

                    } else {
                        alertUserAboutError();
                    }
                }
                catch (IOException e) {
                }
                catch (JSONException e) {
                }
            }
        });
    }
    else {
        Toast.makeText(this, getString(R.string.
                        network_unavailable_message),
                Toast.LENGTH_LONG
        ).show();
    }
}

最佳答案

我假设您正在尝试打印“ CurrentLocation”类的对象。所以值是@ 42c49c78您要做的就像

 Log.i(TAG, "FROM JSON: " + currentLocation.getLatitude());


希望能帮助到你。

10-06 06:38