gpt4 book ai didi

android - getpixel() 显示错误

转载 作者:行者123 更新时间:2023-11-30 04:11:52 24 4
gpt4 key购买 nike

我正在使用一个 ImageView ,我想知道其中的像素颜色,所以我正在使用 getpixel()...方法但它向我展示了一个错误....这就是我使用获取像素方法的方式..

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));

}
}

最佳答案

你得到一个 NullPointerException因为你的k成员是null ;

它被声明并使用 ImageView扩展,并使用 findViewById 初始化返回 null .使用父级 findViewById 中的相同代码 ( Activity )返回所需的 TextView实例。

您应该删除 k来自您的成员(member) FirstImage类,当你得到 pixelonTouch事件,将其传递给 Activity : 让它处理其他 gui 的东西:

所以改变你的FirstImage如:

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

并实现 writePixelIntoK主要 Activity 中的方法,例如:

public void writePixelIntoK(int pixel)
{
k.setText("pixel touched: " + pixel);
}

关于android - getpixel() 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com