如何使用asynctask在google maps api 2上使用特定的ArrayList坐标绘制位图?

时间:2014-01-02 12:04:36

标签: android bitmap android-asynctask

应用程序的想法是从数据库中获取ArrayList<LatLng>,然后使用地图上的位图将这些坐标绘制为标记

我得到了:

NullPointerException at android.graphics.Canvas.drawBitmap(Canvas.java:1195) in onPostExecute

这里有两个问题:

  • 在onPostExecute方法中绘制位图是否正常.. ??

  • 为什么我在onPostExecute的android.graphics.Canvas.drawBitmap(Canvas.java:1195)中得到NullPointerException ... ??

Asynctask Class:

 public class BitmapAsyncTask extends
            AsyncTask<ArrayList<Obstacle>, Void, ArrayList<LatLng>> {
        static Canvas canvas1;
        static BitmapFactory.Options o;
        private Bitmap bmp;
        MarkerOptions balloonmarker;
        private double latitude;
        private double longitude;
        GoogleMap gm;
        LatLng ll;
        LatLng latlong;
        private int len;
        private static int resID = 1;
        static ArrayList<Obstacle> obs;
        static ArrayList<LatLng> coordinates;
        static ArrayList<LatLng> sendingCoord;
        private static double mydirection = 45;
        static Context context;

        public BitmapAsyncTask(Context contextx) {

            context = contextx;
        }

 @Override
 protected ArrayList<LatLng> doInBackground(ArrayList<Obstacle>... params) {
    int i;
    ArrayList<Obstacle> obstacleArray = params[0];
    coordinates = new ArrayList<LatLng>();
    len = obstacleArray.size();
    for (i = 0; i < len; i++) {
        Obstacle obstacle = obstacleArray.get(i);
        latitude = obstacle.getLatitude();
        longitude = obstacle.getLongitude();
        mydirection = obstacle.getDirection();

        }

        for (int x = 0; x < 1; x++) {
            ll = new LatLng(latitude, longitude);
            coordinates.add(ll);
        }
    }

    return coordinates;
}


   @Override
     protected void onPostExecute(ArrayList<LatLng> result) {

             int i;
         int size = result.size();
         bmp = BitmapFactory.decodeResource(context.getResources(),
         R.drawable.map_pin, null);


             bmp=Bitmap.createBitmap(50, 50, Config.ARGB_4444);
         for (i = 0; i < size; i++) {
         latlong = result.get(i);
         canvas1=new Canvas();
             //the exception happens here
         canvas1.drawBitmap(bmp, null, null); 

         balloonmarker = new MarkerOptions().title("MyLocation")
         .snippet("This Is Me").position(latlong).anchor(0, 1)
         .icon(BitmapDescriptorFactory.fromBitmap(bmp));

         gm.addMarker(balloonmarker);
         }


    if (bmp != null) {
        bmp.recycle();
        bmp = null;
                    System.gc();
    }

     }

障碍类:

  public class Obstacle {
       long id;
   double longitude;
   double latitude;
   double direction;
  public Obstacle(double longitude, double latitude, double direction) {
    super();
    this.longitude = longitude;
    this.latitude = latitude;
    this.direction = direction; 
  }
   public Obstacle() {
    // TODO Auto-generated constructor stub
  }
  public double getLongitude() {
    return longitude;
  }
  public void setLongitude(double longitude) {
    this.longitude = longitude;
  }
  public double getLatitude() {
    return latitude;
   }
  public void setLatitude(double latitude) {
    this.latitude = latitude;
  }

  public double getDirection() {
    return direction;
  }
  public void setDirection(double direction) {
    this.direction = direction;
  }

  public Location getLocation() {
    Location myLocation = new Location(MatabbatManager.PROVIDER_STRING);
    myLocation.setLongitude(longitude);
    myLocation.setLatitude(latitude);
    return myLocation;
  }
  }

DataBase Class:

  public class LocalCachedObstacles extends SQLiteOpenHelper {
      static String DATABASE_NAME="localobstaclesdb";
      private static final int DATABASE_VERSION = 1;
      static String OBSTACLES_TABLE ="obstacles";

     private static final String OBSTACLES_TABLE_CREATE ="" +
            "CREATE Table " + OBSTACLES_TABLE +
            "(" +
            "long REAL," +
            "lat REAL," +           
            "direction REAL, " +
            "type REAL, " +
            "address VARCHAR(500)," +
            "time VARCHAR(100)," +
            "submitterName VARCHAR(200) " +
            ")";
     public  ArrayList<Obstacle> getCachedObstacles() {
        try {

            SQLiteDatabase dblocs=this.getReadableDatabase();

            Cursor cur=dblocs.rawQuery("SELECT * FROM "+OBSTACLES_TABLE,new String [] {});
            ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();

            while (cur.moveToNext()) {
                Obstacle obstacle = new Obstacle     

                        (cur.getDouble(0),
                         cur.getDouble(1),cur.getDouble(2),
                         cur.getDouble(3),cur.getString(4),
                 cur.getString(5),cur.getString(6));
                 obstacles.add(obstacle);


            }
            cur.close();
            dblocs.close();
            return obstacles;
        }
        catch (Exception locex){
            Log.e(MatabbatManager.TAG," Get Local cache" + locex.getMessage());
            return  null;
        }


    }

超级课程:

 public class AnonymousUser extends FragmentActivity{
    LocalCachedObstacles lo = new LocalCachedObstacles(
                    MyApplication.getAppContext());
    ArrayList<Obstacle> localObstacles = lo.getCachedObstacles();
    DrawTypes(localObstacles);
    public void DrawTypes(ArrayList<Obstacle> obs) {
            len = obs.size();

        BitmapAsyncTask async = new BitmapAsyncTask(MyApplication.getAppContext());
         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        async.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, obs);
      }
    } 

2 个答案:

答案 0 :(得分:0)

您不能在代码中使用Canvas,我们可以通过覆盖onDraw方法来获取canvas对象。 在我看来,你应该使用叠加来在地图上绘制位图。

答案 1 :(得分:0)

我在尝试了一些与线程threadpool,asynctaskloader相关的所有内容后最终解决了它。 解: 制作服务而不是AsyncTask,因为我需要它继续在后台运行。