0

我正在尝试将 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);
                }
            }
        });

    }

}
4

2 回答 2

0

您没有以正确的方式使用 AsyncTask 。参数的使用是错误的。

  1. 你必须从 doInbackground 返回一些东西到 onPostExecute 方法,可能是一个地址数组,因为你的 doinbackground 只是试图下载一些东西,而你已经在访问数据。尝试阅读堆栈跟踪,它指向您的 217 行代码以及您的 runOnUiThread。在第 217 行,您的变量为空。
  2. onPostExecute 已经是一个 uiThread 方法,因此不应在那里实现可运行对象。就像这样正确地编写代码:
 protected void onPostExecute(List<Address> addressList) {
    for (Address address : addressList) {
        YourActivity.this.displayAddressLines(address);
    }
 }
于 2013-06-20T03:27:26.857 回答
0

问题是您使用了错误的 http 客户端,该客户端已被弃用,并且没有下载任何内容。您的数组“nearPlaces”为空。尝试使用标准的 android http 客户端。读这个:

http://android-developers.blogspot.ru/2011/09/androids-http-clients.html http://developer.android.com/training/basics/network-ops/connecting.html

对于 AsyncTask 用法:您应该像这样更改代码:

class LoadPlaces extends AsyncTask<Void , Void, PlacesList>
{

    /**
     * Before starting background thread Show Progress Dialog
     * */
    protected void onPreExecute()
    {
        super.onPreExecute();

        Log.d(TAG, "onPreExecute");
        /*
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage(Html.fromHtml("Loading Places..."));
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show(); */

    }

    @Override
    protected PlacesList doInBackground(Void... params) {
        Log.d(TAG, "doInBackground");

        // creating Places class object
        googlePlaces = new GooglePlaces();
        try {
            // Separeate your place types by PIPE symbol "|"
            // If you want all types places make it as null
            // Check list of types supported by google
            //
            String types = "bus_station";

            // Radius in meters
            double radius = 1609.34; // 1609.34 meters = 1 mile

            // get nearest places
            nearPlaces = googlePlaces.search(gps.getLatitude(),
                    gps.getLongitude(), radius, types);
            Log.d(TAG, nearPlaces.toString());


        } catch (Exception e) {
            e.printStackTrace();
        }
        return nearPlaces;
    }


    /**
     * After completing background task Dismiss the progress dialog
     * and show the data in UI
     * Always use runOnUiThread(new Runnable()) to update UI from background
     * thread, otherwise you will get error
     * **/
    @Override
    protected void onPostExecute(PlacesList nearPlaces1) {
        super.onPostExecute(nearPlaces1);
        Log.d(TAG, "onPostExecute");

        nearPlaces=nearPlaces1;


        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        // 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>();

                    // Place reference won't display in listview - it will be hidden
                    // Place reference is used to get "place full details"
                    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);
        }
    }
}

这只是使用正确的参数。

关于您的自定义对象:在 android 中,它们应该实现 Parcelable 接口,而不是标准的 java Serializable。 http://developer.android.com/reference/android/os/Parcelable.html

祝你好运!

于 2013-06-21T02:16:54.107 回答