Android:SurfaceView OnClick“不幸的是,.....停止..”

时间:2012-12-17 17:40:57

标签: android

我想将位图实现到surfaceview,并在单击时尝试按按钮编辑图像。但有一个问题。我在模拟器上运行时都是正确的,直到按下按钮。 “不幸的是,.....停止..”出现在屏幕上,程序被迫关闭。请帮我搞清楚。非常感谢你。

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

    SurfaceView surfaceView01 = (SurfaceView) findViewById(R.id.surfaceView1);
    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);
    btn3 = (Button) findViewById(R.id.button3);

    res = getResources();
    bmpDraw =(BitmapDrawable) res.getDrawable(R.drawable.images);
    bmp =bmpDraw.getBitmap();

我想删除这个Onclick方法。但我怎么能声明surfaceView =(SurfaceView)v;没有View v,并将其移动到Oncreat?

    surfaceView01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            surfaceView = (SurfaceView)v;

            canvas = surfaceView.getHolder().lockCanvas();
            Paint paint =new Paint();

            canvas.drawColor(Color.BLACK);

            canvas.drawBitmap(bmp, 50, 150, paint);
            surfaceView.getHolder().unlockCanvasAndPost(canvas);
        }
    });

这里是logcat

12-17 17:24:26.490: E/AndroidRuntime(583): FATAL EXCEPTION: main
12-17 17:24:26.490: E/AndroidRuntime(583): java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.SurfaceView
12-17 17:24:26.490: E/AndroidRuntime(583):  at com.example.effect_ver1.Effect_ver1$2.onClick(Effect_ver1.java:61)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.view.View.performClick(View.java:3480)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.view.View$PerformClick.run(View.java:13983)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.os.Handler.handleCallback(Handler.java:605)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.os.Looper.loop(Looper.java:137)
12-17 17:24:26.490: E/AndroidRuntime(583):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-17 17:24:26.490: E/AndroidRuntime(583):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:24:26.490: E/AndroidRuntime(583):  at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:24:26.490: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-17 17:24:26.490: E/AndroidRuntime(583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-17 17:24:26.490: E/AndroidRuntime(583):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

按钮单击侦听器中的视图v指的是Button的视图而不是SurfaceView。移除surfaceView = (SurfaceView)v;并将surfaceView的所有引用更改为surfaceView01

以下内容对您有用,但我现在无法对其进行测试。

private SurfaceView surfaceView01;

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

    surfaceView01 = (SurfaceView) findViewById(R.id.surfaceView1);
    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);
    btn3 = (Button) findViewById(R.id.button3);

    res = getResources();
    bmpDraw =(BitmapDrawable) res.getDrawable(R.drawable.images);
    bmp =bmpDraw.getBitmap();


    canvas = surfaceView01 .getHolder().lockCanvas();
    Paint paint =new Paint();

    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(bmp, 50, 150, paint);
    surfaceView01 .getHolder().unlockCanvasAndPost(canvas);
}

答案 1 :(得分:0)

在第二次单击侦听器中,您将点击目标转换为SurfaceView。该单击的目标是您添加侦听器的按钮。也就是说,它是一个按钮。按钮无法转换为SurfaceView,因此也是ClassCastException。

额外提示:阅读堆栈跟踪,查看类中的行号(本例中为61)并查找可能导致ClassCastException的内容。在你的情况下,第61行包含一个演员表。它表示v不是SurfaceView,而是Button。此时您应该能够连接点并将该Button与之前使用的Button btn1相关联。

您可能想要使用surfaceView01。您可以将其设置为final或使其成为类成员,以便直接从两个单击侦听器中访问它。