2011-02-23 12 views
2

マップの開始点にマーカーを設定したいと思います。 getResourceがエラーを出していますが、以下はコードです。助けてください。 最後にgetResources()が使用されました。 package infy.route;ここでandroidのgetResources()でバグを取得していますか?

public class MapRouteActivity extends MapActivity { 


    MapView mapView; 
    private Road mRoad; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.routeplanning); 

      mapView = (MapView) findViewById(R.id.mapview); 
      mapView.setBuiltInZoomControls(true); 

      Bundle extras = this.getIntent().getExtras(); 
      String fLat=extras.getString("fromLat"); 
      String fLong=extras.getString("fromLong"); 
      String tLat=extras.getString("toLat"); 
      String tLong=extras.getString("toLong"); 

      final double fromLat=Double.parseDouble(fLat); 
      final double fromLon=Double.parseDouble(fLong); 
      final double toLat=Double.parseDouble(tLat); 
      final double toLon=Double.parseDouble(tLong); 

     new Thread() 
    { 
        @Override 
        public void run() 
        { 



          //double fromLat = 28.6353, fromLon = 77.2250, toLat = 30.7313, toLon = 76.7754; 
          /***url contains the path to fetch the kml file from the internet*/ 
          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 { 
        //Opens a connection to the remote database(bidirectional) 
        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; 
    } 
} 

class MapOverlay extends com.google.android.maps.Overlay { 
    Road mRoad; 
    ArrayList<GeoPoint> mPoints; 
    Drawable greenMarker; 
    Drawable finishMarker; 
    HelloItemizedOverlay itemizedOverlay; 
    List<Overlay> mapOverlays; 
    LinearLayout linearLayout; 


    public MapOverlay(Road road, MapView mv) { 
      mRoad = road; 

      if (road.mRoute.length > 0) 
      { 
        mPoints = new ArrayList<GeoPoint>(); 
        for (int i = 0; i < road.mRoute.length; i++) 
        { 

          mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000), 
              (int) (road.mRoute[i][0] * 1000000))); 

        } 
        int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
            mPoints.size() - 1).getLatitudeE6() - mPoints.get(0) 
            .getLatitudeE6())/2); 
        int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
            mPoints.size() - 1).getLongitudeE6() - mPoints.get(0) 
            .getLongitudeE6())/2); 
        GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong); 

        MapController mapController = mv.getController(); 
        mapController.animateTo(moveTo); 
        mapController.setZoom(7); 
      } 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) { 
      super.draw(canvas, mv, shadow); 
      drawPath(mv, canvas); 
      return true; 
    } 

    public void drawPath(MapView mv, Canvas canvas) { 

     greenMarker = getResources().getDrawable(R.drawable.greenmarker); 

      int x1 = -1, y1 = -1, x2 = -1, y2 = -1; 
      Paint paint = new Paint(); 
      paint.setColor(Color.GREEN); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeWidth(3); 
      mapOverlays = mv.getOverlays(); 
      itemizedOverlay = new HelloItemizedOverlay(greenMarker); 

      for (int i = 0; i < mPoints.size(); i++) { 
        Point point = new Point(); 
        mv.getProjection().toPixels(mPoints.get(i), point); 
        if(i == 0){ 
         OverlayItem overlayitem = new OverlayItem(mPoints.get(0), "", ""); 
         itemizedOverlay.addOverlay(overlayitem); 
         mapOverlays.add(itemizedOverlay);  
        } 
        x2 = point.x; 
        y2 = point.y; 
        if (i > 0) { 
          canvas.drawLine(x1, y1, x2, y2, paint); 
        } 
        x1 = x2; 
        y1 = y2; 
      } 
    } 





} 

は私がのgetResourceを(使用ライン174でスタックトレース である)とdrawpathの168呼び出し()(編集:再フォーマットコード)

02-23 23:20:20.960: ERROR/AndroidRuntime(804): java.lang.NullPointerException 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at infy.route.MapOverlay.drawPath(MapRouteActivity.java:174) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at infy.route.MapOverlay.draw(MapRouteActivity.java:168) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at com.google.android.maps.MapView.onDraw(MapView.java:476) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.View.draw(View.java:6274) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.drawChild(ViewGroup.java:1526) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.drawChild(ViewGroup.java:1524) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.View.draw(View.java:6277) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.drawChild(ViewGroup.java:1526) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.drawChild(ViewGroup.java:1524) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.View.draw(View.java:6277) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1883) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewRoot.draw(ViewRoot.java:1332) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1097) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1613) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.os.Looper.loop(Looper.java:123) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
02-23 23:20:20.960: ERROR/AndroidRuntime(804):  at dalvik.system.NativeStart.main(Native Method) 

行われ

+0

スタックトレースを助けることができる... – Arve

+0

getResources()はnullを返します... ? – Arve

+0

はい、私はそれのためのメソッドを生成している場合、nullを返します。メソッドが生成されていない場合は、エラーを返しますgetresourceはMapOverLayのタイプに対して未定義です – Prachur

答えて

3

オーバーレイを持っていませんリソース。 Context(つまりActivity)への参照を渡し、その代わりにgetResources()を呼び出します。

関連質問:Android getResource() undefined error

EDIT:

public MapOverlay(Road road, MapView mv) { 

MapOverlay mapOverlay = new MapOverlay(MapRouteActivity.this, mRoad, mapView); 

との

変更

MapOverlay mapOverlay = new MapOverlay(mRoad, mapView); 

だから、

Context context; 
public MapOverlay(Context context, Road road, MapView mv) { 
    this.context = context; 

、最終的には、(context.getResourcesへの呼び出しを変更する

)。getDrawable(...

+0

ごめんなさい。申し訳ありませんが、それを手伝ってください。コンテキストを渡してgetResources()を呼び出す方法 – Prachur

+0

@James MapOverlayのコンストラクタは**コンテキストコンテキスト**をパラメータとして取ります。次に、リソースをフェッチするときにcontext.getResources()を呼び出します。 – Arve

+0

更新された回答。 – Arve

関連する問題