没有调用AsyncTask例程

时间:2013-03-08 22:43:10

标签: android asynchronous locationmanager

我试图在Android MainActivity中从我的地图例程中获取gps坐标后运行异步任务。异步任务是执行httpurlconnection以根据gps坐标从我的服务器获取数据。但是,在我的调试过程中观察时,永远不会调用异步任务GetRestaurant()。我应该提一下,在调试器中,循环调用looper例程。在调试中,我注意到程序执行GetRestaurant指令,但之后没有任何反应。从未到达过这门课程。地图确实会显示出来并且是关于它的。任何人都可以从下面的代码中了解为什么我的异步任务类GetRestaurant永远不会被执行?

locationManager.requestLocationUpdates(provider, 3600, 1000, new LocationListener() {

        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}


        public void onLocationChanged(Location loc) {
            if(loc != null) {
                lat = (float) (loc.getLatitude());
                lng = (float) (loc.getLongitude());

                String latLongString = "Lat:" + lat + "\nLong:" + lng;
                myLocationText.setText("Your current position is:\n" + latLongString);
                int latit = (int) (loc.getLatitude() * 1E6);
                int longit = (int) (loc.getLongitude() * 1E6);
                point = new GeoPoint(latit, longit);
                mapController.animateTo(point);
                mapController.setCenter(point);
                mapController.setZoom(15);
                String addr=getAddress(point);
                TextView addressView = (TextView) findViewById(R.id.address_view);
                addressView.setText(addr);
                MyOverlay mapOverlay = new MyOverlay();
                List<Overlay> overlayList=mapView.getOverlays();
                overlayList.clear();
                overlayList.add(mapOverlay);

                new GetRestaurant().execute();

            }

        }
    });

}

 class GetRestaurant extends AsyncTask<Void, Void, String[]> {
 @Override
    protected String[] doInBackground(Void...parameters ) {
        // TODO Auto-generated method stub

        String thislat = ((String.valueOf(lat)));
        String thislng = ((String.valueOf(lng)));
        String[] result = null;
        URL url;


         try {

            String query = "lat=" + thislat + "&lng=" + thislng + "&uid=&vegkey=1";
            url = new URL("http://10.0.2.2:8000/xxxxxxx/webservice/frontpage1.php?" + query);
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            int responseCode = httpConnection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK) {
                InputStream in = httpConnection.getInputStream();
                BufferedReader br = new BufferedReader(
                    new InputStreamReader(in));

                StringBuilder sb = new StringBuilder();

                String line;
                int x=0;
                while ((line = br.readLine()) != null) {
                  //     sb.(line);
                    result[x] = line;
                    x++;
                } 

            }


        }

        catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            System.out.println(e.toString());
        }


    return result;
    }

    protected void onProgressUpdate(Integer... progress) {

    }
    @Override
    protected void onPostExecute(String[] result) {

        restaurants = result;
        final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(null, android.R.layout.simple_list_item_1, restaurants);
        restaurant_list.setAdapter(arrayAdpt);
        mapView.postInvalidate();

    }



  }

这是我的错误日志:我注意到这个错误:“无法在未调用Looper.prepare()的线程内创建处理程序。”这听起来很可疑。

 03-08 23:53:27.846: E/AndroidRuntime(570): FATAL EXCEPTION: AsyncTask #1
 03-08 23:53:27.846: E/AndroidRuntime(570): java.lang.RuntimeException: An error occured while executing doInBackground()
 03-08 23:53:27.846: E/AndroidRuntime(570):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
 03-08 23:53:27.846: E/AndroidRuntime(570):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
 03-08 23:53:27.846: E/AndroidRuntime(570):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at java.lang.Thread.run(Thread.java:856)
  03-08 23:53:27.846: E/AndroidRuntime(570): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.os.Handler.<init>(Handler.java:197)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.os.Handler.<init>(Handler.java:111)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.widget.Toast$TN.<init>(Toast.java:324)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.widget.Toast.<init>(Toast.java:91)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.widget.Toast.makeText(Toast.java:238)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:159)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:1)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
  03-08 23:53:27.846: E/AndroidRuntime(570):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  03-08 23:53:27.846: E/AndroidRuntime(570):    ... 4 more

确定。我将代码更改为在doInBackground末尾读取'return(String [])result.toArray()。现在我收到以下错误:'03 -09 03:24:18.796:E / AndroidRuntime(4726):引起:java.lang.ClassCastException:java.lang.Object []无法强制转换为java.lang.String [ ]”。我认为onPostExecute中发生了错误,其中包含以下代码“

  @Override
    protected void onPostExecute(String[] result) {

    //  restaurants = result;
        final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, result);
        restaurant_list.setAdapter(arrayAdpt);
        mapView.postInvalidate();

    }

如果我可以在调试器中使用该程序但是在我到达问题区域之前它会退出,这会容易得多。

1 个答案:

答案 0 :(得分:0)

看看this answer。它解释了logcat中出现的错误。如果你想清楚地看到你的错误,摆脱吐司;您的System.out输出将显示在logcat中。


问题: 在doInBackground中你有

 String[] result = null;
你在循环中使用的

,没有初始化。我猜您的确实错误是

上的空变量异常
result[x]=line;

线。

扩展解决方案。我希望你能找到你需要改变的地方:

 String line;
 List<String> result = new ArrayList<String>();
 int x=0;
 while ((line = br.readLine()) != null) {
 //     sb.(line);
     result.add(line);

 } 
 return (String[])(result.toArray());