gpt4 book ai didi

安卓:SurfaceView OnClick "Unfortunately, .....stop.."

转载 作者:行者123 更新时间:2023-11-29 21:53:09 26 4
gpt4 key购买 nike

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

这是日志

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)

最佳答案

按钮点击监听器中的 View v 指的是 Button 的 View ,而不是 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);
}

关于安卓:SurfaceView OnClick "Unfortunately, .....stop..",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13919190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com