我想获得一个颜色值bmp图像显示在Textview
上这是我的bmp图片,尺寸为16X24
我按下按钮时尝试按下按钮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)
答案 0 :(得分:1)
NullPointerException 11-13 15:54:56.081:E / AndroidRuntime(11262):
在com.example.bitmapfactory.MainActivity $ 1.onClick
因为mArrayColor
是null
。
在mArrayColor
数组中添加项目之前,需要初始化大小。
如果项目大小未知,请使用ArrayList
代替Array
。