我想在 android 上编写一个简单的图像编辑器。但有一个问题。当我在模拟器上运行它时,一切都是正确的,直到按下按钮。“不幸的是,......停止......”出现在屏幕上,程序被迫关闭。但我真的知道哪里错了。请帮我弄清楚。非常感谢。
这是我的程序代码。
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
draw(drawAction.effect_gray);
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
draw(drawAction.effect_negative);
}
});
}
@Override
protected void onResume() {
super.onResume();
}
enum drawAction {
effect_gray, effect_negative;
}
void draw(drawAction action) {
Canvas canvas = null;
switch (action) {
case effect_gray:
gray(canvas);
break;
case effect_negative:
negative(canvas);
break;
}
}
void gray(Canvas canvas) {
Bitmap vBitmap2 = BitmapFactory.decodeResource( this.getResources()
, R.drawable.images);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(vBitmap2, 0, 0, paint);
img1.setImageBitmap(vBitmap2);
}
void negative(Canvas canvas) {
Bitmap vBitmap2 = BitmapFactory.decodeResource( this.getResources()
, R.drawable.images);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
ColorMatrix cm = new ColorMatrix(new float[] {
-1f, 0f, 0f, 0f, 255f,
0f, -1f, 0f, 0f, 255f,
0f, 0f, -1f, 0f, 255f,
0f, 0f, 0f, 1f, 0f});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(vBitmap2, 0, 0, paint);
img1.setImageBitmap(vBitmap2);
}
}
12-16 16:32:11.741: E/AndroidRuntime(597): FATAL EXCEPTION: main
12-16 16:32:11.741: E/AndroidRuntime(597): java.lang.NullPointerException
12-16 16:32:11.741: E/AndroidRuntime(597): at com.example.a.Aa.gray(Aa.java:81)
12-16 16:32:11.741: E/AndroidRuntime(597): at com.example.a.Aa.draw(Aa.java:64)
12-16 16:32:11.741: E/AndroidRuntime(597): at com.example.a.Aa$1.onClick(Aa.java:37)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.view.View.performClick(View.java:3480)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.view.View$PerformClick.run(View.java:13983)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.os.Handler.handleCallback(Handler.java:605)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.os.Looper.loop(Looper.java:137)
12-16 16:32:11.741: E/AndroidRuntime(597): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-16 16:32:11.741: E/AndroidRuntime(597): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 16:32:11.741: E/AndroidRuntime(597): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 16:32:11.741: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-16 16:32:11.741: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-16 16:32:11.741: E/AndroidRuntime(597): at dalvik.system.NativeStart.main(Native Method)