无法在线程内调用任务

时间:2012-04-06 08:54:53

标签: android multithreading

我正在尝试在地图上显示多个引脚位置,即我的位置以及我从云端服务器获取的其他位置。

基于以下代码:

当我尝试将字符串标记化时,我不断收到NullPointer错误消息。这意味着我的CloudTask活动永远不会被解雇....我无法弄清楚为什么和Eclipse调试器不会逐步完成线程......任何帮助都会受到赞赏。

    public class MapsActivity extends com.google.android.maps.MapActivity {
    private static final String TAG = null;


    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    private MyOverlays itemizedoverlay;
    private MyLocationOverlay myLocationOverlay;
    private MyLocationOverlay otherLocationOverlay;
    private Handler handler;

    private String message;
    StringTokenizer tokens;

    Integer p1 = null;
    Integer p2;

    GeoPoint point;
    static Context mContext = null;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main); // bind the layout to the activity


            // Configure the Map
            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
            mapView.setSatellite(true);
            mapController = mapView.getController();
            mapController.setZoom(14); // Zoon 1 is world view
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2500,
                    0,(LocationListener) new GeoUpdateHandler());
                    //new Intent("android.intent.action.LOCATION_CHANGED"); 
                    //(LocationListener) new GeoUpdateHandler());

            myLocationOverlay = new MyLocationOverlay(this, mapView);


            mapView.getOverlays().add(myLocationOverlay);


            handler = new Handler();
        //  new AsyncTask<Void, Void, String>() {
            myLocationOverlay.runOnFirstFix(new Runnable(){ 
             public void run(){



                    MyRequestFactory requestFactory = Util.getRequestFactory(mContext,
                            MyRequestFactory.class);
                    final CloudTask1Request request = requestFactory.cloudTask1Request();
                    Log.i(TAG, "Sending request to server");


                    request.queryTasks().fire(new Receiver<List<TaskProxy>>() {


                        @Override
                        public void onSuccess(List<TaskProxy> taskList) {
                            //message = result;


                            message = "\n";

                            for (TaskProxy task : taskList) {

                                message += task.getId()+","+task.getNote()+",";                                 

                            }


                        }
                    });
                    //return message;
                //}



                        if(message.length() == 0){

                            Log.i("MESSAGE","Did not get any points from cloud");
                        }

                    tokens = new StringTokenizer(message,",");


                     tokens.nextToken();
                     p1 = Integer.parseInt(tokens.nextToken());
                     p2 = Integer.parseInt(tokens.nextToken());
                     point = new GeoPoint(p1,p2);

                     mapView.getController().animateTo(point);


        }


    });

             Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
                itemizedoverlay = new MyOverlays(this, drawable);
                createMarker();

            myLocationOverlay.runOnFirstFix(new Runnable() {
                public void run() {
                    mapView.getController().animateTo(
                            myLocationOverlay.getMyLocation());
                }
            });

            //Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
            itemizedoverlay = new MyOverlays(this, drawable);
            createMarker();


    }




    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            createMarker();
            mapController.animateTo(point); // mapController.setCenter(point);

        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }


    }

    private void createMarker() {
        GeoPoint p = mapView.getMapCenter();
        OverlayItem overlayitem = new OverlayItem(p, "", "");
        itemizedoverlay.addOverlay(overlayitem);
        if (itemizedoverlay.size() > 0) {
            mapView.getOverlays().add(itemizedoverlay);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass();
    }

    @Override
    protected void onPause() {
        super.onResume();
        myLocationOverlay.disableMyLocation();
        myLocationOverlay.disableCompass();
    }


} 

1 个答案:

答案 0 :(得分:0)

将这些放在onSuccess()

if(message.length() == 0){

    Log.i("MESSAGE","Did not get any points from cloud");
}

tokens = new StringTokenizer(message,",");


tokens.nextToken();
p1 = Integer.parseInt(tokens.nextToken());
p2 = Integer.parseInt(tokens.nextToken());
point = new GeoPoint(p1,p2);

mapView.getController().animateTo(point);