Android:onClick不幸停了下来

时间:2012-12-16 16:13:32

标签: android

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

3 个答案:

答案 0 :(得分:3)

我认为问题在这里

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

最初canvas为null,并且您将canvas作为null传递,它可能会给出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);

答案 1 :(得分:1)

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

void draw(drawAction action, Canvas canvas) {

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

答案 2 :(得分:0)

您不需要enum

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

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) {
       ...
}