Hi, 

I am trying to give my image a clicking effect in Android Studio. The idea is that a color filter is applied when you press the button, and then the color filter gets cleared after they release the button. 

I have tried doing this by using a switch statement inside my OnTouch method, however when I open the app it just crashes.

Here's my code: 

public class MainActivity extends AppCompatActivity implements OnTouchListener {
ImageView imageview = (ImageView) findViewById(R.id.imageView);
SoundPool sp;
int DN_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
DN_id = sp.load(this, R.raw.cow_sound_effect, 1);

}
{
imageview.setOnTouchListener(this);
}



//set the ontouch listener


@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageView view = (ImageView) v;
//overlay is black with transparency of 0x77 (119)
view.getDrawable().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
//clear the overlay
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}

return true;
}
}


When I try to run the app, it tells me that I 'attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference'

If you know what I am doing wrong, please tell me. I would really, really, really appreciate it. (I have been stuck on this for like a month now) 

Sincerely, 


Vidal