使用我自己的纬度和经度时应用崩溃

时间:2016-06-13 16:31:13

标签: java android google-maps cloud

我是java和Android的新手。目前我正在做IOT项目,我必须自己学习Android Studio才能创建应用程序。我使用thingpeak作为云数据库,一切正常。我需要从我的GPS传感器绘制纬度和经度数据中的地图,并且我收到以下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

以下是我的代码。

public class Location extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
public static Double Latitude;
public static Double Longitude;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);

    // 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);

}

//getLatitude method
private class getLatitude extends AsyncTask<String, String, String> {
    String content = "";

    @Override
    protected void onPostExecute(String s) {

        Latitude = Double.parseDouble(s);

    }

    @Override
    protected void onProgressUpdate(String... values) {

    }

    @Override
    protected String doInBackground(String... params) {
        content = getContent();
        return content;
    }

    private String getContent() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        HttpURLConnection urlConnection = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL("https://api.thingspeak.com/channels/channel/fields/1/last?key=APIKey");
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String read;

            while ((read = br.readLine()) != null) {

                sb.append(read);
            }


            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return sb.toString();
    }


}

//getLongitude method
private class getLongitude extends AsyncTask<String, String, String> {
    String content = "";

    @Override
    protected void onPostExecute(String s) {

        Longitude = Double.parseDouble(s);

    }

    @Override
    protected void onProgressUpdate(String... values) {

    }

    @Override
    protected String doInBackground(String... params) {
        content = getContent();
        return content;
    }

    private String getContent() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        HttpURLConnection urlConnection = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL("https://api.thingspeak.com/channels/channel/fields/2/last?key=APIKey");
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String read;

            while ((read = br.readLine()) != null) {
                sb.append(read);
            }


            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return sb.toString();
    }


}

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    new getLatitude().execute("ddd", "fdf", "dfdf");
    new getLongitude().execute("ddd", "fdf", "dfdf");

    // Add a marker and move the camera
    LatLng location = new LatLng(Latitude,Longitude);
    mMap.addMarker(new MarkerOptions().position(location).title("Device Location Marker"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}

}

0 个答案:

没有答案