1

我正在使用一个图像视图,我想在其中知道像素颜色,所以我使用 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));

                }
}
4

1 回答 1

1

你得到一个NullPointerException,因为你的k会员是null

它被声明并使用一个扩展,并使用which 返回 backImageView进行初始化。在父级中使用相同的代码 ( )会返回所需的实例。findViewByIdnullfindViewByIdActivityTextView

你应该k从你的类中删除该成员FirstImage,当你pixel在一个事件中获得值时onTouch,将它传递给Activity: 让它处理其他 gui 的东西:

所以改变你FirstImage的:

pixel = image.getPixel((int)x,(int)y);
((AshActivity) getContext()).writePixelIntoK(pixel);

并在您的主要活动中实现该writePixelIntoK方法,例如:

public void writePixelIntoK(int pixel)
{
    k.setText("pixel touched: " + pixel);
}
于 2012-05-24T10:59:23.397 回答