从mysql显示位置时应用程序崩溃

时间:2016-11-10 15:44:30

标签: android mysql json android-asynctask maps

我的应用程序应该从数据库(在我的情况下从mySQL中)检索标记的经度和纬度,然后在地图上显示它们。在我尝试从Web检索数据之前,它运行良好。有人可以帮帮我吗?其他代码为here

route_activity.java

package com.example.wibisono.tryhard_1;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

public class route_activity extends FragmentActivity implements LocationListener {

    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //show error dialog if GoolglePlayServices not available
        if (!isGooglePlayServicesAvailable()) {
            finish();
        }
        setContentView(R.layout.map_route);
        SupportMapFragment supportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = supportMapFragment.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);

        new RetrieveTask().execute();
    }

    // Adding marker on the GoogleMaps
    //private void addMarker(LatLng latlng) {
      //  MarkerOptions markerOptions = new MarkerOptions();
       // markerOptions.position(latlng);
       // markerOptions.title(latlng.latitude + "," + latlng.longitude);
       // googleMap.addMarker(markerOptions);
    //}

    // Background task to retrieve locations from remote mysql server
    private class RetrieveTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            String strUrl = "http://nfcworld.web.id/public_html/wibisono/retrieve.php";
            URL url = null;
            StringBuffer sb = new StringBuffer();
            try {
                url = new URL(strUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream iStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
                String line = "";
                while( (line = reader.readLine()) != null){
                    sb.append(line);
                }

                reader.close();
                iStream.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sb.toString();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            new ParserTask().execute(result);
        }
    }

    // Background thread to parse the JSON data retrieved from MySQL server
    private class ParserTask extends AsyncTask<String, Void, List<HashMap<String, String>>>{
        @Override
        protected List<HashMap<String,String>> doInBackground(String... params) {
            DirectionsJSONParser markerParser = new DirectionsJSONParser();
            JSONObject json = null;
            try {
                json = new JSONObject(params[0]);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            List<HashMap<String, String>> markersList = markerParser.parse(json);
            return markersList;
        }
        @Override
        protected void onPostExecute(List<HashMap<String, String>> result) {
            for(int i=0; i<result.size();i++){
                HashMap<String, String> marker = result.get(i);
                LatLng latlng = new LatLng(Double.parseDouble(marker.get("lat")), Double.parseDouble(marker.get("lng")));
                //addMarker(latlng);
            }
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        //TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        //googleMap.addMarker(new MarkerOptions().position(latLng));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
        //locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS == status) {
            return true;
        } else {
            GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
            return false;
        }
    }

}

这些是我得到的错误

11-11 00:29:55.443 2008-2482/com.example.wibisono.tryhard_1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                              Process: com.example.wibisono.tryhard_1, PID: 2008
                                                                              java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                  at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                  at java.lang.Thread.run(Thread.java:818)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
                                                                                  at com.example.wibisono.tryhard_1.DirectionsJSONParser.parse(DirectionsJSONParser.java:23)
                                                                                  at com.example.wibisono.tryhard_1.route_activity$ParserTask.doInBackground(route_activity.java:116)
                                                                                  at com.example.wibisono.tryhard_1.route_activity$ParserTask.doInBackground(route_activity.java:106)
                                                                                  at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                  at java.lang.Thread.run(Thread.java:818) 

0 个答案:

没有答案