gpt4 book ai didi

android - 如何在TextView上显示bmp颜色值

转载 作者:行者123 更新时间:2023-11-30 01:46:12 25 4
gpt4 key购买 nike

我想获取在Textview上显示的bmp图片的颜色值

这是我的bmp图片,大小为16X24

enter image description here

我尝试做一个按钮,当按钮被按下时bmp图像颜色值将显示在TextView中

但是按下一个按钮后程序崩溃了

public class MainActivity extends Activity 
{
int mArrayColor[];
int count = 0;
int color;

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView show = (TextView)findViewById(R.id.show);
Button btn = (Button)findViewById(R.id.bitbtn);

btn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.two);
int wid = bitmap.getWidth();
int hei = bitmap.getHeight();


for(int i=0;i<wid;i++)
{
for(int j=0;j<hei;j++)
{
color=bitmap.getPixel(i, j);
mArrayColor[count] = color;
count++;
}
}
show.setText(Arrays.toString(mArrayColor));
}
});
}
}

这是为什么?

这是我的日志

11-13 15:54:56.081: D/AndroidRuntime(11262): Shutting down VM
11-13 15:54:56.081: W/dalvikvm(11262): threadid=1: thread exiting with uncaught exception (group=0x41c8bda0)
11-13 15:54:56.081: E/AndroidRuntime(11262): FATAL EXCEPTION: main
11-13 15:54:56.081: E/AndroidRuntime(11262): Process: com.example.bitmapfactory, PID: 11262
11-13 15:54:56.081: E/AndroidRuntime(11262): java.lang.NullPointerException
11-13 15:54:56.081: E/AndroidRuntime(11262): at com.example.bitmapfactory.MainActivity$1.onClick(MainActivity.java:41)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.view.View.performClick(View.java:4623)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.view.View$PerformClick.run(View.java:19230)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.os.Handler.handleCallback(Handler.java:733)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.os.Looper.loop(Looper.java:157)
11-13 15:54:56.081: E/AndroidRuntime(11262): at android.app.ActivityThread.main(ActivityThread.java:5335)
11-13 15:54:56.081: E/AndroidRuntime(11262): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 15:54:56.081: E/AndroidRuntime(11262): at java.lang.reflect.Method.invoke(Method.java:515)
11-13 15:54:56.081: E/AndroidRuntime(11262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-13 15:54:56.081: E/AndroidRuntime(11262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-13 15:54:56.081: E/AndroidRuntime(11262): at dalvik.system.NativeStart.main(Native Method)

最佳答案

NullPointerException 11-13 15:54:56.081: E/AndroidRuntime(11262):
at com.example.bitmapfactory.MainActivity$1.onClick

因为 mArrayColornull

mArrayColor 数组中添加项目之前需要使用大小进行初始化。

如果项目大小未知,则使用 ArrayList 而不是 Array

关于android - 如何在TextView上显示bmp颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33687998/

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