0

我试图在触摸按钮时使 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);
            }

        });



    }
4

1 回答 1

0

您没有初始化 ImageView 对象,就程序而言,它们只是空引用。

您需要执行以下操作:

//initialize the ImageView
img_left = (ImageView)findViewById(R.id.ID of the image view);
//same thing for img_right
于 2013-01-30T19:21:42.483 回答