意外的应用程序关闭

时间:2011-06-02 08:14:06

标签: android ontouchlistener

我试图启动此代码,以便在onTouch上打印行。该代码不包含任何错误,但是当我启动它时...模拟器意外关闭。你能帮我解决一下这个问题吗?

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Usama3 extends Activity {

        ImageView imageView;
        Bitmap bitmap;
        Canvas canvas;
        Paint paint;

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

        imageView = (ImageView) this.findViewById(R.id.imageView11);

        Display currentDisplay = getWindowManager().getDefaultDisplay();
        float dw = currentDisplay.getWidth();
        float dh = currentDisplay.getHeight();
        bitmap = Bitmap.createBitmap((int)dw,(int)dh,Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        imageView.setImageBitmap(bitmap);
        imageView.setOnTouchListener((OnTouchListener) this);

        }
        public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction();
        float upx;
        float upy;
        float downy = 0;
        float downx = 0;
        switch (action)
        {
            case MotionEvent.ACTION_MOVE:
            upx = event.getX();
            upy = event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            imageView.invalidate();
            downx = upx;
            downy = upy;
            break;
        }
        return false;
}

}

我会非常感激

1 个答案:

答案 0 :(得分:0)

检查Usama3是否在您的AndroidManifest中。您可能还需要让活动明确地实现OnTouchListener而不是尝试类转换this。 Android开发者网站上有一个debugging部分,您可能会觉得这些部分很有用。