I want to get a color value bmp image is displayed on Textview
This is my bmp image, size 16X24
I tried to do a button when the button is pressed bmp image color values will be displayed in the TextView
But after pressing a button the program crashed
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));
}
});
}
}
Why is this?
This's my log
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
Because mArrayColor
is null
.
Need to initialize with size before adding items in mArrayColor
Array.
If item size is not known then use ArrayList
instead of Array
.