我正在尝试将 Google Places 连接到 MapView 但出现错误,它之前工作正常,但在将 Android 库更新到最新版本后出现错误。
请让我知道错误是怎么回事。
06-20 05:41:47.429: E/AndroidRuntime(20755): FATAL EXCEPTION: main
06-20 05:41:47.429: E/AndroidRuntime(20755): java.lang.NullPointerException
06-20 05:41:47.429: E/AndroidRuntime(20755):    at com.x.get2food.MainActivity$LoadPlaces$1.run(MainActivity.java:217)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.app.Activity.runOnUiThread(Activity.java:3717)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at com.x.get2food.MainActivity$LoadPlaces.onPostExecute(MainActivity.java:211)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at com.x.get2food.MainActivity$LoadPlaces.onPostExecute(MainActivity.java:1)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.os.AsyncTask.finish(AsyncTask.java:417)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.os.Looper.loop(Looper.java:123)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at java.lang.reflect.Method.invoke(Method.java:507)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-20 05:41:47.429: E/AndroidRuntime(20755):    at dalvik.system.NativeStart.main(Native Method)
错误区域;
        // calling background Async task to load Google Places
        // After getting places from Google all the data is shown in listview
        // Fired @ onCreate()
        try{
            new LoadPlaces().execute();
        }catch(Exception e){}
/**
 * Background Async Task to Load Google places
 * */
class LoadPlaces extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage(Html.fromHtml("Loading Places..."));
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    /**
     * getting Places JSON
     * */
    protected String doInBackground(String... args) {
        googlePlaces = new GooglePlaces();
        try {
            String types = "bus_station";
            double radius = 1000;
            nearPlaces = googlePlaces.search(gps.getLatitude(),
                    gps.getLongitude(), radius, types);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed Places into LISTVIEW
                 * */
                // Get json response status
                String status = nearPlaces.status;
                // Check for all possible status
                if(status.equals("OK")){
                    // Successfully got places details
                    if (nearPlaces.results != null) {
                        // loop through each place
                        for (Place p : nearPlaces.results) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(KEY_REFERENCE, p.reference);
                            // Place name
                            map.put(KEY_NAME, p.name);
                            // adding HashMap to ArrayList
                            placesListItems.add(map);
                        }
                        // list adapter
                        ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
                                R.layout.list_item,
                                new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                                        R.id.reference, R.id.name });
                        // Adding data into listview
                        lv.setAdapter(adapter);
                    }
                }
                else if(status.equals("ZERO_RESULTS")){
                    // Zero results found
                    alert.showAlertDialog(MainActivity.this, "Near Places",
                            "Sorry no places found. Try to change the types of places",
                            false);
                }
                else if(status.equals("UNKNOWN_ERROR"))
                {
                    alert.showAlertDialog(MainActivity.this, "Places Error",
                            "Sorry unknown error occured.",
                            false);
                }
                else if(status.equals("OVER_QUERY_LIMIT"))
                {
                    alert.showAlertDialog(MainActivity.this, "Places Error",
                            "Sorry query limit to google places is reached",
                            false);
                }
                else if(status.equals("REQUEST_DENIED"))
                {
                    alert.showAlertDialog(MainActivity.this, "Places Error",
                            "Sorry error occured. Request is denied",
                            false);
                }
                else if(status.equals("INVALID_REQUEST"))
                {
                    alert.showAlertDialog(MainActivity.this, "Places Error",
                            "Sorry error occured. Invalid Request",
                            false);
                }
                else
                {
                    alert.showAlertDialog(MainActivity.this, "Places Error",
                            "Sorry error occured.",
                            false);
                }
            }
        });
    }
}