我正在尝试让imageView在按钮被触摸时向上移动,当按钮被释放时,我希望它重新向下移动。我开始写一些示例代码,至少让它向上移动,但我的onClick中已经有一个空指针。有什么想法吗?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class Test extends Activity {
ImageView img_left;
ImageView img_right;
Button left;
Button right;
TranslateAnimation moveup;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
moveup = new TranslateAnimation(0, 0, 0, 300);
moveup.setDuration(3000);
moveup.setFillAfter(true);
left = (Button) findViewById(R.id.left);
right = (Button) findViewById(R.id.right);
left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_left.startAnimation(moveup);
}
});
}发布于 2013-01-31 03:21:42
您不是在初始化ImageView对象,就程序而言,它们只是空引用。
你需要做一些类似的事情:
//initialize the ImageView
img_left = (ImageView)findViewById(R.id.ID of the image view);
//same thing for img_righthttps://stackoverflow.com/questions/14611844
复制相似问题