1

我想在 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)
4

3 回答 3

3

我认为这里有问题

Canvas canvas = null;
switch (action) {
case effect_gray:
     gray(canvas);
    break;
case effect_negative:
    negative(canvas);
     break;
}

最初 canvas 为 null 并且您将 canvas 作为 null 它可能会给出空指针异常。有关更多信息,请分享堆栈跟踪..

改写成这样

void draw(drawAction action, Canvas canvas) 
{
   if(canvas != null)
   { 
       switch (action) {
          case effect_gray:
          gray(canvas);
          break;
       case effect_negative:
          negative(canvas);
          break;
    }
}

并像这样调用这个方法:

draw(drawAction.effect_gray,canvas);
于 2012-12-16T16:19:00.893 回答
1

将方法中的Canvas实例draw作为参数传入

void draw(drawAction action, Canvas canvas) {

    if(canvas != null)
    {
        switch (action) {
            case effect_gray:
                gray(canvas);
                break;
            case effect_negative:
                negative(canvas);
                break;
        }
    }
}
于 2012-12-16T16:25:20.933 回答
0

你不需要一个enum.

使用类成员来保存 Canvas 实例,然后根据需要调用gray和方法。negative

private Canvas mCanvas;

onCreate(Bundle bundle)
{
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            gray(mCanvas);
        }
    });

    btn2.setOnClickListener(new OnClickListener() {  
        public void onClick(View arg0) {
             negative(mCanvas);
        }
    });
}

...

void gray(Canvas canvas) {
    ...
}

void negative(Canvas canvas) {
       ...
}
于 2012-12-16T16:33:29.940 回答