SHUBHAM DHINGRA SHUBHAM DHINGRA - 2 months ago 27
Java Question

Error: does not override abstract method onClick(View) in OnClickListener

private OnClickListener onButtonClick = new OnClickListener() {
// @Override
public void OnClick(View v) {
switch (v.getId()) {
case R.id.play: {
if (player.isPlaying()) {
handler.removeCallbacks(updatePositionRunnable);
player.pause();
playButton.setImageResource(android.R.drawable.ic_media_play);
} else {
if (isStarted) {
player.start();
playButton.setImageResource(android.R.drawable.ic_media_pause);

updatePosition();
} else {
startPlay(currentFile);
}
}
break;
}
case R.id.next: {
int seekto = player.getCurrentPosition() + STEP_VALUE;

if (seekto > player.getDuration())
seekto = player.getDuration();
player.pause();
;
player.seekTo(seekto);
player.start();

break;
}
case R.id.prev: {
int seekto = player.getCurrentPosition() - STEP_VALUE;
if (seekto < 0)
seekto = 0;

player.pause();
player.seekTo(seekto);
player.start();

break;
}

}

}

};

private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
};

private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
// @Override
public boolean OnError(MediaPlayer mp, int what, int extra) {
return false;
}
};

private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
//@Override
public void OnStopTrackingTouch(SeekBar seekBar) {
isMoveingSeekBar = false;
}

//@Override
public void OnStartTrackingTouch(SeekBar seekBar) {
isMoveingSeekBar = true;
}

//@Override
public void OnProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isMoveingSeekBar)
{
player.seekTo(progress);
Log.i("OnSeekBarChangedListener", "OnProgressChanged");
}
}
};
}


I'm getting this after gradle build:


Error:(189, 67) error: is not abstract and
does not override abstract method onClick(View) in OnClickListener

Error:(248, 85) error: is not abstract and
does not override abstract method onError(MediaPlayer,int,int) in
OnErrorListener

Error:(255, 100) error: is not abstract and
does not override abstract method onStopTrackingTouch(SeekBar) in
OnSeekBarChangeListener

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.


Compilation failed; see the compiler error output for details.


Answer

Each of the methods that have the // @Override line before them are not the correct method names for each interface. As @laalto pointed out, each of these method names should start with "on" instead of "On". Then the @Override annotation will not produce an error (as it is overriding the correct method), and you can uncomment them. This should be the correct code:

private OnClickListener onButtonClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play:
                if (player.isPlaying()) {
                    handler.removeCallbacks(updatePositionRunnable);
                    player.pause();
                    playButton.setImageResource(android.R.drawable.ic_media_play);
                else {
                    if (isStarted) {
                        player.start();
                        playButton.setImageResource(android.R.drawable.ic_media_pause);
                        updatePosition();
                    } else {
                        startPlay(currentFile);
                    }
                }
                break;
            case R.id.next:
                int seekto = player.getCurrentPosition() + STEP_VALUE;

                if (seekto > player.getDuration()) {
                        seekto = player.getDuration();
                }

                player.pause();
                player.seekTo(seekto);
                player.start();

                break;
            case R.id.prev:
                int seekto = player.getCurrentPosition() - STEP_VALUE;
                if (seekto < 0) {
                    seekto = 0;
                }

                player.pause();
                player.seekTo(seekto);
                player.start();

                break;
        }
    }
};

private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        stopPlay();
    }
};

private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }
};

private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        isMoveingSeekBar = false;
    }

    @Override
    public void OnStartTrackingTouch(SeekBar seekBar) {
        isMoveingSeekBar = true;
    }

    @Override
    public void OnProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (isMoveingSeekBar) {
            player.seekTo(progress);
            Log.i("OnSeekBarChangedListener", "OnProgressChanged");
        }
    }
};

Android Studio also has bunch of autocomplete features that will take care of implementing interface methods for you, you should use them to your advantage. In this way you should be able to start typing the name of the interface, and then you should see a prompt like this:

enter image description here

Then you can just press return (enter on Windows) to autocomplete the implementation:

enter image description here