I am making an android app, where, during startup I disable the pauze button of the media player. After the ok button is clicked (and music starts to play), I want to enable the pauze button.
This code works fine, except for the last line (I did delete some non-relevant lines). That gives me a runtime error (NullPointerException).
I used MainActivity.this to access the button. What am I doing wrong?
Thank you for any help!
public class MainActivity extends Activity {
// initialise variables
TextView textOut;
MediaPlayer mediaPlayer;
Button buttonPauze;
Button ok;
// *******************************************************
// set Layout - on create
// *******************************************************
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOut = (TextView) findViewById(R.id.textView1);
// *******************************************************
// set the play, generate and pauze buttons
// *******************************************************
Button buttonPauze = (Button) findViewById(R.id.buttonPauze);
Button ok = (Button) findViewById(R.id.buttonStart);
buttonPauze.setOnClickListener(buttonPauseOnClickListener);
buttonPauze.setEnabled(false);
ok.setEnabled(true);
// *******************************************************
// on click generate
// *******************************************************
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.buttonPauze.setEnabled(true);
Replace:
with:
Basically, you had 2
buttonPauze‘s, one as local variable and the other as a class field. By removing theButton, you are using the class fieldbuttonPauzelocally and assign a proper object to it.Same thing with
ok!