2012-07-03 168 views
0

當我使用此代碼找到兩個地理點之間的距離,他們之間繪製路徑,我得到這個例外java.lang.IllegalArgumentException異常:InputStream中不能爲空

java.lang.IllegalArgumentException: InputStream cannot be null 

public class MapRouteActivity extends MapActivity { 
    LinearLayout linearLayout; 
    MapView mapView; 
    private Road mRoad; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     new Thread() { 
      @Override 
      public void run() { 
       double fromLat = 49.85, fromLon = 24.016667; 
       double toLat = 50.45, toLon = 30.523333; 
       String url = RoadProvider 
         .getUrl(fromLat, fromLon, toLat, toLon); 
       InputStream is = getConnection(url); 
       mRoad = RoadProvider.getRoute(is); 
       mHandler.sendEmptyMessage(0); 
      } 
     }.start(); 
    } 

    Handler mHandler = new Handler() { 
     public void handleMessage(android.os.Message msg) { 
      TextView textView = (TextView) findViewById(R.id.description); 
      textView.setText(mRoad.mName + " " + mRoad.mDescription); 
      MapOverlay mapOverlay = new MapOverlay(mRoad, mapView); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay); 
      mapView.invalidate(); 
     }; 
    }; 

    private InputStream getConnection(String url) { 
     InputStream is = null; 
     try { 
      URLConnection conn = new URL(url).openConnection(); 
      is = conn.getInputStream(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return is; 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 
+2

你有互聯網許可嗎? – Blackbelt

+0

好吧它的工作...非常感謝 –

+0

歡迎您 – Blackbelt

回答

1

InputStream爲空,你需要修復:

InputStream is = getConnection(url); 
if (is != null) { 
    // do your work 
} 
else { 
    // logging error 
} 

在你的情況你的方法,如果你有權限上網getConnection(url)返回null所以首先檢查:

<uses-permission android:name="android.permission.INTERNET"/> 
相關問題