如何在TextView上显示bmp颜色值
How to display bmp color value on TextView
我想得到一个颜色值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.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.onClick
因为mArrayColor
是null
。
在 mArrayColor
数组中添加项目之前需要使用大小进行初始化。
如果项目大小未知,则使用 ArrayList
而不是 Array
。
我想得到一个颜色值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.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.onClick
因为mArrayColor
是null
。
在 mArrayColor
数组中添加项目之前需要使用大小进行初始化。
如果项目大小未知,则使用 ArrayList
而不是 Array
。