我从网络服务获取经度和纬度,并在谷歌地图上显示结果(标记)..但有时我得到以下错误......应用程序强行关闭...... 我该如何解决/处理这个......? 建议非常感谢...
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): java.lang.NullPointerException
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at com.google.android.maps.MapView.onTouchEvent(MapView.java:679)
09-16 14:51:41.359: ERROR/AndroidRuntime(27858): at android.view.View.dispatchTouchEvent(View.java:3932)
我的代码显示叠加层:
class backgroundLoadResults extends
AsyncTask<Void, Void, Void> {
Handler mHandler = new Handler(Looper.getMainLooper());
String message = null;
double mylat = 0 ,mylon = 0;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
//getListView().addHeaderView(buildHeader());
dialog.dismiss();
if(markerssList.getLatitude().size() < 0 ||markerssList.getLatitude().size() == 0 )
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(SActivity.this);
alertbox.setMessage("No result found..Please try later..");
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
tb2.setEnabled(true);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mHandler.post(new Runnable() {
public void run() {
dialog = ProgressDialog.show(SActivity.this, "",
"Showing the results,Please wait...", true);
dialog.show();
}
});
}
protected void onProgressUpdate(Integer... progress) {
setProgress(progress[0]);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
geocoder = new Geocoder(SActivity.this); //<3>
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if ( !locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER ) ) {
//buildAlertMessageNoGps();
}
if (location != null)
{
message = String.format("\n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
mylat = location.getLatitude() ;
mylon = location.getLongitude();
}
System.out.println(""+mylat+mylon);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// mylat = -33.853468 ;
// mylon = 150.94042160000004;
item = spinner.getSelectedItemPosition();
int radi = 0;
if(item == 1)
{radi = 5;}
if(item == 2)
{radi = 10155;}
if(item == 3)
{radi = 10155;}
System.out.println("just above the urll"+mylat+mylon);
/** Send URL to parse XML Tags */
URL sourceUrl = new URL("XXXXXXXXXXXXXXXXXXX?lat="+mylat+"&lng="+mylon+"&radius="+radi);
System.out.println(" the urll "+sourceUrl);
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}
catch (Exception e) {
}
markerssList = MyXMLHandler.markerList;
System.out.println("lenghttttt"+markerssList.getLatitude().size());
Drawable drawable = ShowroomLocatorActivity.this.getResources().getDrawable(R.drawable.icon);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, ShowroomLocatorActivity.this);
for (int i = 0; i < markerssList.getLatitude().size(); i++) {
// **Is this correct??**
ss1 = markerssList.getLatitude().get(i);
ss2 = markerssList.getLongitude().get(i);
ss3 = markerssList.getName().get(i);
ss4 = markerssList.getAddress().get(i);
ss5 = markerssList.getCity().get(i);
System.out.println("lat...."+ ss1);
System.out.println("llogitude......."+ ss2);
Log.i("latitude..." + i, ss1);
Log.i("longitude.."+i,ss2);
{
p = new GeoPoint((int) (Double.parseDouble(ss1) * 1E6),
(int) (Double.parseDouble(ss2) * 1E6));
OverlayItem overlayItem = new OverlayItem(p, " ",
""+ss3+"\n"+ss4+"\n"+ss5);
itemizedOverlay.addOverlay(overlayItem);
//mapController.animateTo(p);
//mapController.setZoom(17);
}
catch(Exception e1)
{System.out.println("newwww"+e1);}
}
mapView.getOverlays().add(itemizedOverlay);
mapController = mapView.getController();
mapController.animateTo(p);
mapController.setZoom(10);
mapView.postInvalidate();
return null;
}
}
}
我的onTap叠加方法:
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setIcon(R.drawable.icon);
dialog.setNegativeButton("go to direction", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.out.println("get directions..."+ss4+ss5);
} });
dialog.setNeutralButton("buisness card", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
} });
//dialog.setPositiveButton("Get Direction", this);
dialog.show();
return true;
}
答案 0 :(得分:1)
我有完全相同的错误;在触摸地图时。
原因;我发现在arraylist中缺少至少一个重叠项目。
您可能需要向itemizedOverlay集合添加默认的overlayitem。检查你的课程初始化地点,并尝试做这样的事情。
overlays = mapV.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.someicon);
Test1ItemizedOverlay testingMapsItemizedOverlay = new Test1ItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(55791764,12377150);
OverlayItem overlayitem = new OverlayItem(point, "First overlay item!", "Here, I am!");
testingMapsItemizedOverlay.addOverlay(overlayitem);
overlays.add(testingMapsItemizedOverlay);
答案 1 :(得分:1)
你可以试试这个:
if(testingMapsItemizedOverlay.size > 0)
overlays.add(testingMapsItemizedOverlay);