0

我又遇到了 Android 中的 AsyncTask 的问题。

我制作了一个简单的 AsyncTask 类来在 MapView 上显示多个标记。但是开始活动,有一个 NullPointerException - 我认为这是由 MapView 引起的......

public class MapDetail extends MapActivity {

// JSON Node names
private static final String TAG_NEWNAME = "name";
private static final String TAG_LONG = "long";
private static final String TAG_LAT = "lat";

// Variablen
private MapController mc;
private List<Overlay> mapOverlays;
private Drawable drawable;
private MapItemizedOverlay itemizedoverlay;
private MapView mapView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.offers);

    // getting intent data
    Bundle extras = getIntent().getExtras();
    String url = extras.getString("url");

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mc = mapView.getController();

    mapOverlays = mapView.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.marker);
    itemizedoverlay = new MapItemizedOverlay(drawable, this);

    new MyAsyncTask().execute(url);

}

这是我的异步任务:

public class MyAsyncTask extends AsyncTask<String, Void, MapItemizedOverlay > {


    @Override
    protected MapItemizedOverlay doInBackground(String... params) {
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();
        JSONArray locations = jParser.getJSONArrayFromUrl(params[0]);

        for (int i = 0; i < locations.length(); i++) {
            JSONObject c;
        try {
            c = locations.getJSONObject(i);
            // Storing each json item in variable
            String firma = c.getString(TAG_NEWNAME);
            String longitude = c.getString(TAG_LONG);
            String latitude = c.getString(TAG_LAT);
            System.out.println("Kriege ich daten"+longitude+"und"+latitude);
            double x = Double.parseDouble(latitude);
            double y = Double.parseDouble(longitude);


            GeoPoint point = new GeoPoint((int)(x * 1E6),(int)(y * 1E6) );
            OverlayItem overlayitem = new OverlayItem(point, firma, null);
            itemizedoverlay.addOverlay(overlayitem);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
        System.out.println(itemizedoverlay.toString());
        return itemizedoverlay;
    }

    @Override
    protected void onPostExecute(MapItemizedOverlay result) {
        mapOverlays.add(result);
        mapView.getOverlays().add(result);
        LocationManager myLocationManager;
        LocationListener myLocationListener;            
        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();

        myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);

        GeoPoint initGeoPoint = new GeoPoint(   (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()*1000000),
                                                (int)(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()*1000000));

        mc.animateTo(initGeoPoint);
        mc.setZoom(6); 
        mapView.invalidate();

    }

}}

我的日志:

    01-06 19:49:23.703: E/AndroidRuntime(6064): FATAL EXCEPTION: main
    01-06 19:49:23.703: E/AndroidRuntime(6064): java.lang.NullPointerException
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at de.main.MapDetail$MyAsyncTask.onPostExecute(MapDetail.java:150)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at de.main.MapDetail$MyAsyncTask.onPostExecute(MapDetail.java:1)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.os.AsyncTask.finish(AsyncTask.java:631)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.os.Looper.loop(Looper.java:137)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at java.lang.reflect.Method.invoke(Method.java:511)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-06 19:49:23.703: E/AndroidRuntime(6064):     at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0