0

I am trying to implement the get color method for the cwac color mixer( https://github.com/commonsguy/cwac-colormixer), but every time this code runs I get a NullPointerException:

int color = 1;

public void openColor(){
ColorMixer mixer = (ColorMixer)findViewById(R.id.mixer);
  if (color!=1){
    mixer.setColor(color);
  }
  color = mixer.getColor();
}

The color = mixer.getColor(); is the line that crashes.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
jcw
  • 5,132
  • 7
  • 45
  • 54
  • You're most likely not searching for the `ColorMixer` in the right place(or your layout doesn't have a `ColorMixer` element with the id `R.id.mixer`). – user Jul 02 '12 at 19:14
  • Thank you luksprog - it turned out it was crashing because I was trying to run the openColor method within a pop up window, and what I needed to do was not use a separate method and instead put the name of my pop up window (see http://stackoverflow.com/questions/8595427/android-popup-and-button-to-dismiss-doesnt-work) - I will add this as an answer as soon as I can (people like me who can't answer any questions can't post an answer within 8 hours) – jcw Jul 02 '12 at 19:26

2 Answers2

1

I haven't ever used this Commonsware component before, but from looking at the source code, it appears all you need to do is set up a listener/callback. This mechanism will allow you to get notified of any changes in the color picking.

First, have your class implement the following listener interface:

public interface OnColorChangedListener {
    public void onColorChange(int argb);
}

Then set the listener on the ColorMixer using:

public void setOnColorChangedListener(OnColorChangedListener listener) {
    this.listener=listener;
}

If you do it all inline, it will look somewhat like this:

mixer.setOnColorChangeListener(new ColorMixer.OnColorChangeListener() {
    @Override public void onColorChange(int argb) {
        //... get the color here; e.g.
        color = argb;
    }
});
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Sorry for wasting your time - found the answer, just have to wait 8 hours before answering my own question – jcw Jul 02 '12 at 19:50
0

Thank you luksprog - it turned out it was crashing because I was trying to run the openColor method within a pop up window, and what I needed to do was not use a separate method and instead put the name of my pop up window (see Android: popUp and button to dismiss doesnt work)

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54