1

我想将位图实现到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)
4

2 回答 2

2

按钮单击侦听器中的 View v 指的是 Button 的视图,而不是 SurfaceView。删除并更改对tosurfaceView = (SurfaceView)v;的所有引用surfaceViewsurfaceView01

以下应该对你有用,但我现在没有办法测试它。

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);
}
于 2012-12-17T17:59:44.537 回答
0

在您的第二个单击侦听器中,您将单击的目标投射到 SurfaceView。该单击的目标是您添加侦听器的按钮。也就是说,它是一个按钮。按钮不能被投射到 SurfaceView,因此 ClassCastException。

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

您可能想改用surfaceView01。您可以将其设为 final 或将其设为类成员以直接从两个点击侦听器中访问它。

于 2012-12-17T17:52:11.223 回答