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