0

我坚持这个。希望您能够帮助我。我给自己弄了一个地方应用程序。而且我仍然不断收到使用它的人的错误。我找不到解决方案。所以这是我得到的错误信息:

java.lang.NullPointerException
at com.laurenswuyts.find.it.MainActivity$LoadPlaces$1.run(MainActivity.java:297)
at android.app.Activity.runOnUiThread(Activity.java:4631)
at com.laurenswuyts.find.it.MainActivity$LoadPlaces.onPostExecute(MainActivity.java:291)
at com.laurenswuyts.find.it.MainActivity$LoadPlaces.onPostExecute(MainActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4918)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)

这是我主要活动的一部分:

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

                    if (nearPlaces.results != null) {
                    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);

                                //Distance
                                double dist, dist2, dist3, dist4;
                                int dist5;
                                String distance, distance2, distance3;
                                Double piem1, piem2;
                                String piem3, piem4;
                                double lat1 = gps.getLatitude();
                                double lng1 = gps.getLongitude();
                                double lat2 = p.geometry.location.lat;
                                double lng2 = p.geometry.location.lng;

                                dist = distance(lat1,lng1,lat2,lng2);

                                if(dist < 1) {
                                    double meterConversion = 1.609;
                                    double feetConversion = 5.208;
                                    String locale = Locale.getDefault().getISO3Country();
                                    if(locale.equals("USA")){
                                        dist2 = dist * feetConversion;
                                        dist3 = round(dist2,3);
                                        dist4 = dist3 * 1000;
                                        dist5 = (int)dist4;
                                        distance = String.valueOf(dist5);                                   
                                        distance2 = distance + " ft";
                                    }
                                    else{
                                    dist2 = dist * meterConversion;
                                    dist3 = round(dist2,3);
                                    dist4 = dist3 * 1000;
                                    dist5 = (int)dist4;
                                    distance = String.valueOf(dist5);                                   
                                    distance2 = distance + " m";}
                                }
                                else {


                                dist3 = round(dist,1);

                                String locale = Locale.getDefault().getISO3Country();
                                if(locale.equals("USA")){
                                    distance = Double.toString(dist3);
                                    distance2 = distance + " mi";
                                }
                                else{
                                    double meterConversion = 1.609;
                                    dist2 = dist * meterConversion;
                                    dist3 = round(dist2,1);
                                    distance = Double.toString(dist3);
                                distance2 = distance + " km";
                                }
                                }

                                piem1 = p.geometry.location.lat;
                                piem2 = p.geometry.location.lng;
                                piem3 = Double.toString(piem1);
                                piem4 = Double.toString(piem2);
                                distance3 = piem3 + "," + piem4;
                                map.put(KEY_DISTANCE, distance2);

                                // Place name
                                map.put(KEY_NAME, p.name);




                                //Vicinity
                                map.put(KEY_VICINITY, p.vicinity);

                                //Location
                                map.put(KEY_LOCATION, distance3);


                                    if(p.photos != null){
                                map.put(KEY_TRUE, "speeltijd");
                                map.put(KEY_IMAGE, "https://maps.googleapis.com/maps/api/place/photo?maxwidth=65&photoreference="+p.photos[0].photo_reference +"&sensor=true&key=AIzaSyBqUEZXSjwqbXGfEJEO99rQAL8puy11GVg");
                            }else{
                                map.put(KEY_TRUE, "school");
                                    }
                                placesListItems.add(map);

                            }

                            lv=(ListView)findViewById(R.id.list);


                            // Getting adapter by passing xml data ArrayList
                            adapter = new LazyAdapter(MainActivity.this, placesListItems);
                            lv.setAdapter(adapter);

                            // list adapter
                        /** ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
                                    R.layout.list_item,
                                    new String[] { KEY_REFERENCE, KEY_NAME, KEY_VICINITY, KEY_DISTANCE, KEY_LOCATION}, new int[] {
                                            R.id.reference, R.id.name, R.id.vicinity, R.id.radius, R.id.location});

                            // Adding data into listview
                            lv.setAdapter(adapter);**/




                        }
                        else
                        {
                            alert.showAlertDialog(MainActivity.this, "Places Error",
                                    "Sorry error occured.",
                                    false);


                        }

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


                    }
                    }else {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry unknown error occured.",
                                false);
                        }
                }
            });


        }

第 291 行开始于 runOnUiThread(new Runnable() 即第 291 行。

提前致谢

4

3 回答 3

2

onPostExecute()已经在 UI 上Thread。您不需要调用runOnUiThread(). 另外,是第 297 行if(nearPlaces.results != null) {...吗?此时 nearPlaces 是否为空?

于 2013-09-11T16:49:18.900 回答
0

您正在 asynctask 的帖子中更新 UIThread ..

AsyncTask 必须子类化才能使用。子类将覆盖至少一种方法(doInBackground(Params...)),并且通常会覆盖第二种方法(onPostExecute(Result)。)

检查这个:异步任务

你正在做的事情在里面runOnUiThread()doInBackground,然后把结果发布到onPostExecute

于 2013-09-11T16:53:35.437 回答
0

您的 nearPlaces 为空。核实。

于 2013-09-11T17:09:40.280 回答