0

因此,我进行了连接测试并停止了对地图引脚的 JSON 调用。但是,即使这样做了,如果用户点击地图,应用程序也会崩溃。

它似乎在 Google API 的 maps.jar 中为触摸事件调用一个方法,但我无权访问这些对吗?

那么如何测试它或者有更好的方法来检查它?

public class Map_Activity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */

MapView map;
private MyLocationOverlay me = null;
Drawable d;
List<Overlay> overlayList;
LocationManager lm;
String towers;
int lat;
int longi;

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

    map = (MapView) findViewById(R.id.mapview);

    Touchy t = new Touchy();
    overlayList = map.getOverlays();
    overlayList.add(t);

    // //////////////////////////////////// Where the maps opens up to and
    // zoom
    map.getController().setCenter(getPoint(40.801531, -81.405661));
    map.getController().setZoom(15);

    Drawable marker = getResources().getDrawable(R.drawable.pin_yellow);

    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
            marker.getIntrinsicHeight());

    map.getOverlays().add(new SitesOverlay(marker));

    me = new MyLocationOverlay(this, map);
    map.getOverlays().add(me);

    // d = getResources().getDrawable(R.drawable.pin_blue);

    // Geo Location of phone
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    towers = lm.getBestProvider(crit, false);
    Location location = lm.getLastKnownLocation(towers);

    if (location != null) {
        lat = (int) (location.getLatitude() * 1E6);
        longi = (int) (location.getLongitude() * 1E6);
        // GeoPoint ourLocation = new GeoPoint(lat, longi);
        // OverlayItem overlayItem = new OverlayItem(ourLocation,
        // "Quit hitting your self", "");
        // CustomPinpoint custom = new CustomPinpoint(d, Map_Activity.this);
        // custom.insertPinpoint(overlayItem);
        // overlayList.add(custom);
    } else {
        // Toast.makeText(MapsActivity.this, "Couldn't get provider",
        // Toast.LENGTH_SHORT).show();
    }

    initLocation();
}

private void initLocation() {

    lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 500, this);
}

@Override
public void onResume() {
    super.onResume();
    me.enableCompass();
    lm.requestLocationUpdates(towers, 500, 1, this);
}

@Override
public void onPause() {
    super.onPause();
    me.disableCompass();
    lm.removeUpdates(this);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class Touchy extends Overlay {
    public boolean onTouchEvent(MotionEvent e, MapView m) {

        return false;
    }
}

private GeoPoint getPoint(double lat, double lon) {
    return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> items = new ArrayList<OverlayItem>();

    public SitesOverlay(Drawable marker) {
        super(marker);

        if (isNetworkAvailable() == true) {
            boundCenterBottom(marker);
            // //////////////////////////////////////////////////////////////////////
            String result = queryRESTurl("http://www.mywesite.com/json.json");
            Log.e("Spot 1", "");
            JSONObject json;
            JSONObject json2;
            // JSONObject data;
            try {
                json = new JSONObject(result);
                JSONArray nameArray = json.names();

                JSONArray valArray = json.toJSONArray(nameArray);

                for (int i = 0; i < valArray.length(); i++) {

                    Log.e(nameArray.getString(i), valArray.getString(i));
                    json2 = new JSONObject(valArray.getString(i));
                    JSONArray nameArray2 = json2.names();

                    JSONArray valArray2 = json2.toJSONArray(nameArray2);
                    for (int a = 0; a < valArray2.length(); a++) {

                        // add to maps here
                        items.add(new OverlayItem(getPoint(
                                valArray2.getDouble(3),
                                valArray2.getDouble(2)), valArray2
                                .getString(1), valArray2.getString(0)));
                        Log.e(nameArray2.getString(a),
                                valArray2.getString(a));
                    }
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Pin Locations

            populate();
        } else {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    Map_Activity.this);
            ad.setIcon(android.R.drawable.ic_dialog_alert);
            ad.setTitle("OH NO!");
            ad.setMessage("To view the latest information you need a data or wi-fi connection");
            ad.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    });
            ad.show();
        }
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (items.get(i));
    }

    @Override
    protected boolean onTap(int i) {
        // Toast.makeText(MapsActivity.this,
        // items.get(i).getSnippet(),Toast.LENGTH_SHORT).show();
        OverlayItem item = items.get(i);
        AlertDialog.Builder dialog = new AlertDialog.Builder(
                Map_Activity.this);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return (true);
    }

    @Override
    public int size() {
        return (items.size());
    }
}

public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    List<Overlay> overlays = map.getOverlays();
    me = new MyLocationOverlay(this, map);
    overlays.add(me);
    me.enableMyLocation();
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public String queryRESTurl(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;

    try {
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            String result = RestClient.convertStreamToString(instream);
            instream.close();
            return result;
        }
    } catch (ClientProtocolException e) {
        Log.e("REST", "There was a protocol based error", e);
    } catch (IOException e) {
        Log.e("REST", "There was an IO Stream related error", e);
    }

    return null;
}

/*
 * Check for Connectivity
 */
public boolean isNetworkAvailable() {
    Context context = getApplicationContext();
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {

    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
}

在此处输入图像描述

4

0 回答 0