更改对象的引用会导致先前的对象数据被覆盖

时间:2012-10-09 13:24:11

标签: java android

我似乎对java对象的引用有问题,可能是因为在C中进行了多年的编程。以下代码可以让我在画布周围移动“WordPart”的对象。当我选择第一个对象时,它会按预期移动。当我释放第一个对象并选择第二个对象时,第一个和第二个对象的位置变得相同。 (我已经删除了编译所需的代码,但与问题无关。)据我所知,activeImage引用似乎同时指向wordPartList中的两个对象。请帮助我找出我的误解。

...
public class ONG2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SurfaceView2(this));
}

public class SurfaceView2 extends SurfaceView implements SurfaceHolder.Callback {    

ArrayList<WordParts> wordPartList;
WordParts activeImage;
Point activePosition;
String TAG = "SurfaceView2";
public SurfaceView2(Context context) {
super(context);        
wordPartList = NewPartList(context);
this.getHolder().addCallback(this);
setFocusable(true);
activeImage = null;
activePosition = new Point();
}
/**
 * 
 * @return list of word parts
 */
private ArrayList<WordParts> NewPartList(Context context) {
ArrayList<WordParts> newParts = new ArrayList<WordParts>();
newParts.add(new WordParts(context, new Point(400, 100), "foo"));
newParts.add(new WordParts(context, new Point(200, 100), "bar");
return newParts;
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "inside onDraw");
canvas.drawColor(Color.BLACK);
if (activeImage != null) {
activeImage.NewPosition(activePosition);
}
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(100, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(200, 300));
draw(canvas, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), new Point(300, 300));
for (WordParts wp : wordPartList) {
Point center = wp.getCenter();
Log.d(TAG, String.format("Word part %s at x=%d, y=%d", wp.mWordPartString, center.x, center.y));
draw(canvas, wp.getBitMap(), center);
}

}     
@Override    
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, String.format("inside on touch, event is %s", event.toString()));
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// if within wordPart, set as active
for (WordParts wp : wordPartList) {
if (wp.isInside(event)) {
activeImage = wp;
Log.d(TAG, String.format("Touch down inside word part %s", wp.mWordPartString));
break; // end iteration
}
}

break;
case MotionEvent.ACTION_UP:
for (WordParts wp : wordPartList) {
if (activeImage == wp){
wp.endTouch();
Log.d(TAG, String.format("Released word part %s", wp.mWordPartString));
}
}
activeImage = null;
break;

default:
break;
}
activePosition.x = (int) event.getX();
activePosition.y = (int) event.getY();
if (activeImage != null)
this.postInvalidate(); // force call to onDraw
return true;
}

/**
 * Draw the bitmap centered at specified point
 * @param canvas - target of drawing
 * @param bm - bitmap to draw
 * @param centerPt
 */
public void draw(Canvas canvas, Bitmap bm, Point centerPt) {
canvas.drawBitmap( bm, 
   centerPt.x - (bm.getWidth() / 2), 
   centerPt.y - (bm.getHeight() / 2), null); 
Log.d(TAG, String.format("canvas %s, bm %s, point %s", canvas.toString(), bm.toString(), centerPt.toString()));
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) 
{     

}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("surface", "Surface created");        
setWillNotDraw(false);
            }
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("surface", "Surface destroyed");
}
}
}

1 个答案:

答案 0 :(得分:3)

怀疑这就是问题:

if (activeImage != null) {
    activeImage.NewPosition(activePosition);
}

很遗憾,我们没有非常规命名NewPosition的代码,但可能会设置WordParts中字段的值。如果您在两个单独的WordParts个对象中设置两个字段的值以引用相同的Point,那么稍后当您更改Point内的值时,就像这样:

activePosition.x = (int) event.getX();
activePosition.y = (int) event.getY();

然后是的,这些更改通过两个WordParts对象可见,因为它们都引用同一个对象。

如果您是Java新手,我会强烈建议您通过简单的控制台应用程序学习该语言的基础知识。了解对象,引用和原语,继承,核心集合和IO等,然后开始在UI和移动的棘手环境中工作。