getpixel()显示错误

时间:2012-05-24 08:06:57

标签: android getpixel

我正在使用图像视图,其中我想知道像素颜色,所以我使用getpixel()... 方法 但它给我一个错误.... 这是我使用get pixel方法的方式..

image  = BitmapFactory.decodeResource(getResources(), rid);
                pixel = image.getPixel((int)x,(int)y);
                k.setText(Integer.toString(pixel)); 

它向我显示错误.......

05-24 13:29:38.101: E/AndroidRuntime(21987): FATAL EXCEPTION: main
05-24 13:29:38.101: E/AndroidRuntime(21987): java.lang.NullPointerException
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.example.nam.FirstImage.pageinfo(FirstImage.java:100)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.example.nam.FirstImage.onTouch(FirstImage.java:63)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.View.dispatchTouchEvent(View.java:3881)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1769)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1135)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1753)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2227)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.os.Looper.loop(Looper.java:130)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at java.lang.reflect.Method.invoke(Method.java:507)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-24 13:29:38.101: E/AndroidRuntime(21987):    at dalvik.system.NativeStart.main(Native Method)

编辑::::

public class FirstImage extends ImageView implements OnTouchListener {


    MotionEvent event;
    TextView k;
    int rid;
    int pixel;
    Bitmap image;
    String huma ="human";
    String info = "human";
     float x = 0; //init value 
     float y = 0; //init value
     Animation animationFadeIn;
    int t=0;
    int i=1;

    public FirstImage(Context context) { 
        super(context); 
        k=(TextView)findViewById(R.id.textView1);



        } 
    public FirstImage(Context context, AttributeSet attrs) { 
        super(context, attrs);
        k=(TextView)findViewById(R.id.textView1);
        } 


    public void changeImage(int id){

                    this.setImageResource(id);
                rid=id;
                    final Animation animationFadeout=AnimationUtils.loadAnimation(getContext(), R.anim.zoomin);
                    this.startAnimation(animationFadeout);
                    this.setOnTouchListener(this);

    }
    @Override
    public boolean onTouch(View arg0, MotionEvent me) {

        switch(me.getAction()){
        case MotionEvent.ACTION_DOWN:
            x=me.getX();
            y= me.getY();
            pageinfo(x,y);
            break;
        case MotionEvent.ACTION_MOVE:
            x=me.getX();
            y= me.getY();
            pageinfo(x,y);
            break;
        case MotionEvent.ACTION_UP:
            x=me.getX();
            y= me.getY();
            pageinfo(x,y);
            break;
        case MotionEvent.ACTION_OUTSIDE:
            x=me.getX();
            y= me.getY();
            pageinfo(x,y);
            break;
        default: return true;
        }
        return false;
    }
    public void pageinfo(float x, float y) {
        // TODO Auto-generated method stub

         if(rid==R.drawable.cell){
                image  = BitmapFactory.decodeResource(getResources(), rid);
                pixel = image.getPixel((int)x,(int)y);
                k.setText(Integer.toString(pixel));

                }
}

1 个答案:

答案 0 :(得分:1)

您收到NullPointerException,因为您的k成员是null;

声明并使用ImageView扩展,并使用返回findViewById的{​​{1}}进行初始化。 在父null中使用相同的代码(findViewById)会返回所需的Activity实例。

您应该从TextView课程中删除k成员,并在FirstImage事件中获得pixel值后,将其传递给onTouch }:让它处理其他gui的东西:

因此请将Activity更改为:

FirstImage

并在主要活动中实施pixel = image.getPixel((int)x,(int)y); ((AshActivity) getContext()).writePixelIntoK(pixel); 方法,例如:

writePixelIntoK