2012-11-30 154 views
0

這是我的項目,您可以在列表視圖中點擊醫院的名稱,並在下一個活動中顯示細節,下面的細節是mapview。我的項目崩潰,我得到這個錯誤的logcat搜索功能Mapview

11-30 18:22:50.085: E/AndroidRuntime(13114): FATAL EXCEPTION: main 
11-30 18:22:50.085: E/AndroidRuntime(13114): java.lang.NullPointerException 
11-30 18:22:50.085: E/AndroidRuntime(13114): at com.dr.droid.lee.HospResult$GeocoderTask.onPostExecute(HospResult.java:130) 
11-30 18:22:50.085: E/AndroidRuntime(13114): at com.dr.droid.lee.HospResult$GeocoderTask.onPostExecute(HospResult.java:1) 
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask.finish(AsyncTask.java) 
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask.access$600(AsyncTask.java) 
11-30 18:22:50.085: E/AndroidRuntime(13114): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java) 

這裏是我的我的課

public class HospResult extends MapActivity { 

    String hcity; 
    String hregion; 
    String hadd; 
    String hname; 
    String hcon; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     Typeface type = Typeface.createFromAsset(this.getAssets(),"MyriadPro-Bold.ttf"); 
     setContentView(R.layout.map); 
     TextView hosname = (TextView) findViewById(R.id.tvName); 
     TextView hosreg = (TextView) findViewById(R.id.tvRegion); 
     TextView hosadd = (TextView) findViewById(R.id.tvAddress); 
     TextView hoscon = (TextView) findViewById(R.id.tvContact); 
     TextView hoscity = (TextView) findViewById(R.id.tvCity); 
     Bundle receive = getIntent().getExtras(); 
     hcity = receive.getString("city"); 
     hregion = receive.getString("region"); 
     hadd = receive.getString("address"); 
     hname = receive.getString("name"); 
     hcon = receive.getString("contact"); 
     hosname.setText(hname); 
     hosreg.setText(hregion); 
     hoscity.setText(hcity); 
     hosadd.setText(hcon); 
     hoscon.setText(hadd); 
     hosname.setTypeface(type); 
     hosreg.setTypeface(type); 
     hosadd.setTypeface(type); 
     hoscity.setTypeface(type); 
     hoscon.setTypeface(type); 
     //MapView mv = (MapView) findViewById(R.id.mv1); 
     //MapController mp = mv.getController(); 
     //mp.setZoom(15); 
     String loc = hosname.getText().toString(); 
     //Toast.makeText(this, loc, Toast.LENGTH_SHORT).show(); 

     if(loc!=null && !loc.equals("")){ 
      new GeocoderTask().execute(loc); 
     } 


    } 

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

    // An AsyncTask class for accessing the GeoCoding Web Service 
     private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{ 

      @Override 
      protected List<Address> doInBackground(String... locationName) { 
       // Creating an instance of Geocoder class 
       Geocoder geocoder = new Geocoder(getBaseContext()); 
       List<Address> addresses = null; 

       try { 
        // Getting a maximum of 3 Address that matches the input text 
        addresses = geocoder.getFromLocationName(locationName[0], 1); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       }   
       return addresses; 
      } 


      @Override 
      protected void onPostExecute(List<Address> addresses) { 

       // Getting Reference to MapView of the layout activity_main 
       MapView mapView = (MapView) findViewById(R.id.mv1); 

       // Setting ZoomControls 
       mapView.setBuiltInZoomControls(true); 

       // Getting MapController for the MapView 
       MapController mc = mapView.getController(); 
       mc.setZoom(18); 

       // Getting Drawable object corresponding to a resource image 
       Drawable drawable = getResources().getDrawable(R.drawable.marker); 


       // Getting Overlays of the map 
       List<Overlay> overlays = mapView.getOverlays(); 

       // Creating an ItemizedOverlay 
       LocationOverlay locationOverlay = new LocationOverlay(drawable,getBaseContext()); 

       // Clearing the overlays 
       overlays.clear(); 

       if(addresses==null || addresses.size()==0){ 
        Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show(); 

        // Redraws the map to clear the overlays 
        mapView.invalidate(); 
       } 

       // Adding Markers on Google Map for each matching address 
       for(int i=0;i<addresses.size();i++){     

        Address address = (Address) addresses.get(i); 

        // Creating an instance of GeoPoint, to display in Google Map 
        GeoPoint p = new GeoPoint(
               (int)(address.getLatitude()*1E6), 
               (int)(address.getLongitude()*1E6) 
              ); 

        String addressText = String.format("%s, %s", 
          address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", 
          address.getCountryName()); 


        // Creating an OverlayItem to mark the point 
        OverlayItem overlayItem = new OverlayItem(p, "Location",addressText); 

        // Adding the OverlayItem in the LocationOverlay 
        locationOverlay.addOverlay(overlayItem); 

        // Adding locationOverlay to the overlay 
        overlays.add(locationOverlay); 

        // Locate the first location 
        if(i==0) 
         mc.animateTo(p);      
       } 

       // Redraws the map 
       mapView.invalidate(); 

      }  
     } 

} 

線130點開始在這裏

for(int i=0;i<addresses.size();i++){ 

加入這個搜索之類的函數我的項目之前的代碼正在顯示一張地圖並運行罰款,但是在添加了這個搜索功能之後,我從教程中獲得了它的功能,它崩潰了。任何想法爲什麼我的項目在加載地圖時崩潰?我該如何解決這個問題?

回答

1

嘗試 intialize與List<Address> addresses = new List<Address>();

爲@Carnal十分正確地在這裏說你DNT必須初始化您的列表,如果你確信你是通過該方法獲得geocoder.getFromLocationName名單,那麼你可以使用List<Address> addresses=null或者您可以在列表當您正在使用它們進行循環時,請檢查列表大小和使用情況。

+0

即時得到一個錯誤,當我添加了一行代碼。紅線下新列表

() – leenolasco

0

你不需要初始化List<Address> addresses = new List<Address>();,你仍然可以有List<Address> addresses = null,因爲geocoder.getFromLocationName返回List<Address>。問題似乎是,在執行循環之前,您不檢查您的地址列表是否爲空。這似乎會導致程序崩潰。

做此項檢查:

if(addresses != null && addresses.size() > 0){ 
    for(int i=0;i<addresses.size();i++){ 
     // rest of your code.. 
    } 
} 
+0

其奇怪後,我拔掉我的設備從我的電腦它工作正常。感謝您的迴應先生! – leenolasco

+0

是的,但仍然需要檢查,「getFromLocatioName」 - 方法可以返回null,如果是這樣的話,你的應用程序會崩潰! – Carnal