2013-02-06 1 views
0

5 개의 연결된 좌표로 Google지도를 보여주는 앱을 만들고 있습니다. 아이콘으로 표시 할 수 있습니다. 이제 우리는 마커를 클릭 할 때 대화 상자를 표시하려고하는데 오류가 발생합니다. 아래 코드를 찾으십시오. 어떤 도움이라도 대단히 감사 할 것입니다.안드로이드에서 클릭하는 동안 맵 마커가 던지고 있습니다.

이것은 내 메인 클래스입니다.

public class MapConnect extends MapActivity implements LocationListener { 

    private static final String TAG = "LocationActivity"; 
// public List<Overlay> mapOverlays; 
// public MyOwnLocationOverlay myLocationOverlay; 
    public static MapConnect instance; 
    public MapView mapView; 
    protected LocationManager locationManager; 
    public Button retrieveLocationButton; 
    Geocoder geocoder; 
    TextView locationText; 
    Location location; 
    MapController mapController; 
    CountDownTimer locationtimer; 
    GeoPoint point1 = new GeoPoint(29391748,13436279); 
    GeoPoint point2 = new GeoPoint(28835050,14128418); 
    GeoPoint point3 = new GeoPoint(28256006,13941650); 
    GeoPoint point4 = new GeoPoint(28401065,13029785); 
    GeoPoint point5 = new GeoPoint(29027355,12546387); 

     // private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
// private static final long MINIMUM_TIME_BETWEEN_UPDATES = 9000; // in Milliseconds 
// MapOverlay mapOverlay = new MapOverlay(); 
    @Override   
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_map_connect); 

     mapView = (MapView) findViewById(R.id.map_view); 

     MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); 
     mapView.getOverlays().add(myLocationOverlay); 
     mapView.postInvalidate(); 
     myLocationOverlay.enableMyLocation(); 
     mapView.setBuiltInZoomControls(true); 
     locationText = (TextView)this.findViewById(R.id.lblLocationInfo); 
     mapController = mapView.getController(); 
     mapView.setSatellite(false); 
     mapController.setZoom(8); 
     retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button); 

//  GeoPoint point1 = new GeoPoint(29391748,13436279); 
//  GeoPoint point2 = new GeoPoint(28835050,14128418); 
//  GeoPoint point3 = new GeoPoint(28256006,13941650); 
//  GeoPoint point4 = new GeoPoint(28401065,13029785); 
//  GeoPoint point5 = new GeoPoint(29027355,12546387); 
      MapOverlay mapOvlay = new MapOverlay(point1, point2,point3,point4,point5); 
      //mapController.setCenter(point4); 
      mapView.getOverlays().add(mapOvlay); 

      List<Overlay> mapOverlays = mapView.getOverlays(); 
      Drawable drawable = this.getResources().getDrawable(R.drawable.pin); 
      HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable); 
      OverlayItem overlayitem = new OverlayItem(point1, null, null); 
      OverlayItem overlayitem1 = new OverlayItem(point2, null, null); 
      OverlayItem overlayitem2 = new OverlayItem(point3, null, null); 
      OverlayItem overlayitem3 = new OverlayItem(point4, null, null); 
      OverlayItem overlayitem4 = new OverlayItem(point5, null, null); 
      itemizedoverlay.addOverlay(overlayitem); 
      itemizedoverlay.addOverlay(overlayitem1); 
      itemizedoverlay.addOverlay(overlayitem2); 
      itemizedoverlay.addOverlay(overlayitem3); 
      itemizedoverlay.addOverlay(overlayitem4); 
      mapOverlays.add(itemizedoverlay); 


    } 

    public class MapOverlay extends com.google.android.maps.Overlay { 

     private GeoPoint mGpt1; 
     private GeoPoint mGpt2; 
     private GeoPoint mGpt3; 
     private GeoPoint mGpt4; 
     private GeoPoint mGpt5; 

     protected MapOverlay(GeoPoint gp1, GeoPoint gp2,GeoPoint gp3,GeoPoint gp4,GeoPoint gp5) { 
      mGpt1 = gp1; 
      mGpt2 = gp2; 
      mGpt3 = gp3; 
      mGpt4 = gp4; 
      mGpt5 = gp5; 
      mapController.setCenter(gp3); 
     } 
     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
       long when) { 
      super.draw(canvas, mapView, shadow); 
      Paint paint; 
      paint = new Paint(); 
      paint.setColor(Color.RED); 
      paint.setAntiAlias(true); 
      paint.setStyle(Style.STROKE); 
      paint.setStrokeWidth(2); 
      Point pt1 = new Point(); 
      Point pt2 = new Point(); 
      Point pt3 = new Point(); 
      Point pt4 = new Point(); 
      Point pt5 = new Point(); 
      Projection projection = mapView.getProjection(); 
      projection.toPixels(mGpt1, pt1); 
      projection.toPixels(mGpt2, pt2); 
      projection.toPixels(mGpt3, pt3); 
      projection.toPixels(mGpt4, pt4); 
      projection.toPixels(mGpt5, pt5); 
      canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint); 
      canvas.drawLine(pt2.x, pt2.y, pt3.x, pt3.y, paint); 
      canvas.drawLine(pt3.x, pt3.y, pt4.x, pt4.y, paint); 
      canvas.drawLine(pt4.x, pt4.y, pt5.x, pt5.y, paint); 
      canvas.drawLine(pt5.x, pt5.y, pt1.x, pt1.y, paint); 
      return true; 
     } 

    } 


//  public void drawMyLocation(Canvas canvas,MapView mapView,location,point,Long 1000); 

     public void showsat(View view){ 
     mapView.setSatellite(true); 
     } 

     public void showcurrentlocation(View view) { 

//  mapView.getOverlays().clear(); 
//  mapOverlays = mapView.getOverlays(); 
//  
//  myLocationOverlay = new MyOwnLocationOverlay(GMapsActivity.this, mapView); 
//  myLocationOverlay.setMeters(10);  
//  myLocationOverlay.enableCompass();   
//  myLocationOverlay.enableMyLocation();   
//  myLocationOverlay.runOnFirstFix(new Runnable() {  
//   public void run() {    
//    mapController.animateTo(myLocationOverlay.getMyLocation());  
//    }  
//   });  
//  mapView.getOverlays().add(myLocationOverlay); } 
     mapView.setSatellite(false);   
     geocoder = new Geocoder(MapConnect.this); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     // add this overlay to the MapView and refresh it 


     if (location != null) {  
      Log.d(TAG, location.toString()); 
      this.onLocationChanged(location); //<6> 

      } 

     } 
//  
     @Override 
     public void onBackPressed() { 
      // do something on back. 
      MapOverlay mapOvlay = new MapOverlay(point1, point2,point3,point4,point5); 
      mapView.getOverlays().add(mapOvlay); 
      // finish(); 
      return; 
     }  
     public void finish() { 
      // do something on back. 
      finish(); 
      return; 
     }   

    @Override 
      public void onLocationChanged(Location location) { 
      Log.d(TAG, "onLocationChanged with location " + location.toString()); 
      String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),location.getLongitude(), location.getAltitude(), 
        location.getBearing()); 
      this.locationText.setText(text); 

        try {  
         List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> 
         for (Address address : addresses) {  
          this.locationText.append("\n" + address.getAddressLine(0)); 
          }   
         int latitude = (int)(location.getLatitude() * 1000000); 
         int longitude = (int)(location.getLongitude() * 1000000); 
         GeoPoint point = new GeoPoint(latitude,longitude);  
         mapController.animateTo(point); //<11> 


        } 
        catch (IOException e) { 
         Log.e("LocateMe", "Could not get Geocoder data", e); 
         } 
//   
    } 
        @Override 
        public void onProviderDisabled(String provider) { 
         // TODO Auto-generated method stub 

        } 

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

        } 

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


        } 


//     @Override protected void onResume() { 
//     LocationListener locationListener = new LocationListener(){ 
//      super.onResume(); 
//     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7> 
//     } 
//    @Override protected void onPause() { 
//     super.onPause(); 
//     locationManager.removeUpdates(this); //<8> 
//      
//     } 


//@Override 
//protected void onResume() { 
// super.onResume(); 
// 
// // when our activity resumes, we want to register for location updates 
// 
// 
// myLocationOverlay.enableMyLocation(); 
// 
//} 
// 
//@Override 
//protected void onPause() { 
// super.onPause(); 
// 
// // when our activity pauses, we want to remove listening for location updates 
// 
// myLocationOverlay.disableMyLocation(); 
// 
//} 



     @Override 
     protected boolean isRouteDisplayed() { 
      return false; 

     } 
} 

이제 항목 별 오버레이 클래스 :

public class HelloItemizedOverlay extends ItemizedOverlay { 

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
    private Context mContext; 

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

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

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

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

// @Override 
// protected boolean onTap(int index) { 
//  AlertDialog.Builder dialog = new AlertDialog.Builder(MapConnect.instance); 
// switch (index) { 
//   case 0: 
//    Toast.makeText(MapConnect.instance, "GeoLocation : 0", 
//      Toast.LENGTH_LONG).show(); 
//    dialog.show(); 
//   break; 
//  } 
// 
//  return true; 
// } 



    @Override 
    protected boolean onTap(final int index) { 

       final OverlayItem id = mOverlays.get(index); 
       AlertDialog.Builder dialog = new AlertDialog.Builder(MapConnect.instance); 
       dialog.setTitle("your title"); 
       dialog.setMessage("youmessage"); 
       //dialog.setNegativeButton("Cancel", null); 
       dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int index) { 
        dialog.dismiss(); 
        } 
       }); 
       dialog.show(); 
       return true; 

       } 




    } 

이 내가 로그 캣 오류 얻고있다 :

2-06 07:17:34.822: E/InputEventReceiver(6112): Exception dispatching input event. 
02-06 07:17:34.822: E/MessageQueue-JNI(6112): Exception in MessageQueue callback: handleReceiveCallback 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): java.lang.NullPointerException 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.example.mapconnect.HelloItemizedOverlay.onTap(HelloItemizedOverlay.java:58) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:358) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:562) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.MapView.onTouchEvent(MapView.java:685) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.View.dispatchTouchEvent(View.java:7239) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
+0

코드를 참조하는 첫 번째 줄에 대한 예외를 찾으십시오. 여기 뭐야? 'com.example.mapconnect.HelloItemizedOverlay.onTap (HelloItemizedOverlay.java:58)' – Simon

답변

0

어리석은 실수를 저질렀 기 때문에 답을 얻었습니다. 주요 활동에 대한 컨텍스트를 초기화하지 않았습니다. Oncreate에서 아래에 추가 한 후 작동했습니다. instance = MapConnect.this

0

이 시도, 대신에이 문제를 해결할 수 null 빈 값의 희망

OverlayItem overlayitem = new OverlayItem(point1, "", ""); 
+0

안녕 Murali, 나는 시도했지만 여전히 같은 오류가 발생합니다. 두 번째 클래스에서 내 Alertdialog 생성에 문제가있을 것입니다. – manishpro

+0

안녕하세요, 어리석은 실수를 저의 답으로 받았습니다. 주요 활동에 대한 컨텍스트를 초기화하지 않았습니다. Oncreate에서 아래에 추가 한 후 작동했습니다. instance = MapConnect.this; – manishpro