0

我在触摸地图中的搜索位置图标时收到此错误...我有 3 个位置。2 位置 r 我的位置和客户位置 & 我有搜索编辑框。当我搜索该位置时,它在正确的位置显示 d 图标,但我触摸该图标将强制关闭...我必须得到对话框。这是我的代码 谁能指导我如何使用 JSON 在两个位置之间获取 Google 路线

请帮助我提前谢谢

08-27 17:52:52.708: D/AndroidRuntime(3721): Shutting down VM
08-27 17:52:52.708: W/dalvikvm(3721): threadid=1: thread exiting with uncaught exception (group=0x40be71f8)
08-27 17:52:52.723: E/AndroidRuntime(3721): FATAL EXCEPTION: main
08-27 17:52:52.723: E/AndroidRuntime(3721): java.lang.NullPointerException
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.androidhive.googleplacesandmaps.AddItemizedOverlay.onTap(AddItemizedOverlay.java:74)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:356)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:683)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.View.dispatchTouchEvent(View.java:5588)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2060)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1398)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2008)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.View.dispatchPointerEvent(View.java:5768)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2911)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2485)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:852)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2494)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.os.Looper.loop(Looper.java:137)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at android.app.ActivityThread.main(ActivityThread.java:4514)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at java.lang.reflect.Method.invoke(Method.java:511)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-27 17:52:52.723: E/AndroidRuntime(3721):     at dalvik.system.NativeStart.main(Native Method)
08-27 17:52:59.911: I/Process(3721): Sending signal. PID: 3721 SIG: 9

Placesmapactivity.java

上面的 tr 是我的完整代码的链接,只需使用此方法以供快速参考..

       @Override
            public void onClick(View v) {           
                String usrLoc = autoCompletetextView.getText().toString();
                Geocoder gc = new Geocoder(PlacesMapActivity.this);

                try {
                    addr=gc.getFromLocationName(usrLoc, 3);//maximum 2 results
                    if(addr.size()>0){
                        address=addr.get(0);//get the first result
                        convertLongi=address.getLongitude();
                        convertLati=address.getLatitude();
                        geopointsearch = new GeoPoint((int)(convertLati * 1E6), (int)(convertLongi * 1E6));                                     

                     // Map geopointsearch overlay item
                        itemizedOverlay2 = new AddItemizedOverlay(drawable_client);     
                        overlayitem = new OverlayItem(geopointsearch, "Your Location2", "I am ur client!");
                        itemizedOverlay2.addOverlay(overlayitem);       
                        mapOverlays.add(itemizedOverlay2);
                        itemizedOverlay2.populateNow();
                        mc.animateTo(geopointsearch);   
                     //   mc.zoomIn();
                    }

                } catch (IOException e) {                   
                    e.printStackTrace();
                }
            }                                   
        });

    // Map overlay item
    itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
    overlayitem = new OverlayItem(geoPoint, "Your Location","That is you!");
    itemizedOverlay.addOverlay(overlayitem);        
    mapOverlays.add(itemizedOverlay);
    itemizedOverlay.populateNow();      
    // Map client overlay item
    itemizedOverlay1 = new AddItemizedOverlay(drawable_client, this);       
    overlayitem1 = new OverlayItem(geoPoint1, "Your Location2", "I am ur client!");
    itemizedOverlay1.addOverlay(overlayitem1);              
    mapOverlays.add(itemizedOverlay1);
    itemizedOverlay1.populateNow();
    // Map near overlay item    
    itemizedOverlay = new AddItemizedOverlay(drawable, this);
    // Map geopointsearch overlay item
   /* itemizedOverlay2 = new AddItemizedOverlay(drawable_client, this);     
    overlayitem2 = new OverlayItem(geopointsearch, "Your Location3", "ur searching for me!");
    itemizedOverlay2.addOverlay(overlayitem2);      
    mapOverlays.add(itemizedOverlay2);
    itemizedOverlay2.populateNow();*/

AddItemizedOverlay.java

公共类 AddItemizedOverlay 扩展 ItemizedOverlay {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;
   MapController mc;
Drawable drawable;

private GeoPoint geopoint;



public AddItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
   }

   public AddItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
   }

   @Override
   public boolean onTouchEvent(MotionEvent event, MapView mapView)
   {   
       if (event.getAction() == 1) {
           GeoPoint geopoint = mapView.getProjection().fromPixels(
               (int) event.getX(),
               (int) event.getY());
           // latitude
           double lat = geopoint.getLatitudeE6() / 1E6;
           // longitude
           double lon = geopoint.getLongitudeE6() / 1E6;

       //  Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
       }     
       return false;
   } 


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

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

   @Override
   protected boolean onTap(int index) {
     OverlayItem item = mapOverlays.get(index);
     AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
     dialog.setTitle(item.getTitle());
     dialog.setMessage(item.getSnippet());
     dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
     dialog.show();
     return true;
   }

   public void addOverlay(OverlayItem overlay) {
      mapOverlays.add(overlay);
   }

   public void populateNow(){
       this.populate();
   }

}
4

1 回答 1

0

尝试更改受保护的布尔 onTap(int index) 如下:

@Override
protected boolean onTap(int index) {
 OverlayItem item = mapOverlays.get(index);
 AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
 dialog.setTitle(item.getTitle());
 dialog.setMessage(item.getSnippet());
 dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
 AlertDialog alertDialog=dialog.create();
 alertDialog.show();
 return true;

}

您需要从 DialogBu​​ilder 创建 AlertDialog。

于 2012-08-27T13:12:06.053 回答