我有以下代码:
if(prevPoint != null){
Log.i("tracking class", "prevPoint: "+ prevPoint);
Point pointb = null;
Projection projection2 = mapView.getProjection();
projection2.toPixels(prevPoint, pointb);
Log.i("tracking class", "Pointb: "+ pointb);
Paint paint;
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Log.i("tracking class", "Point x: "+ point.x);
Log.i("tracking class", "Point y: "+ point.y);
Log.i("tracking class", "Pointb x: "+ pointb.x);
Log.i("tracking class", "Pointb y: "+ pointb.y);
//canvas.drawLine(pointb.x, pointb.y, point.x, point.y, paint);
}
日志返回:
10-19 08:55:25.505: INFO/tracking class(29970): prevPoint: 41310173,-105558377
10-19 08:55:25.505: INFO/tracking class(29970): Pointb: null
为什么Pointb会回来null?
答案 0 :(得分:2)
您是否希望Java使用pass by reference?它没有。它在任何地方都使用pass by value,尽管引用类型的任何表达式的值都是引用。
调用
projection2.toPixels(prevPoint, pointb);
无法可能更改pointb
的值。您需要使用赋值运算符,例如
Point pointb = projection2.toPixels(prevPoint, null);
我相信toPixels
通过参数更改现有对象引用的的内容,如果参数为非null ...但它不可能更改用于引用新对象的变量。