Android : Button with sound OnClick causing NullPointerException

on Wednesday, March 25, 2015


I am new to java and I am creating a button that once you press, you get a sound-simple stuff. My code is this inside MainActivity:



ImageButton one = (ImageButton) new Button(R.id.imageButton); //error here
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pears);
one.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
mp.start();
}
});


I get an error "in Button cannot be applied to int" but my variable is not type of int but a Button type. Also a fatal error with a NullPointerException.I looked up NullPointerException and seems like you get the error when an object is not created. But I am creating an object of ImageButton... Any advice? Thanks


0 comments:

Post a Comment