Andreas Heimann
Andreas Heimann

Reputation: 63

MediaPlayer Cannot resolve method create

Problem solved! I had a very dumb fail in the .xml where i forgot an "@+id/".. also i had to clear the project so this annoying error with R cannot be resolved got away..


After i tries some codes it left the error.. it also displays a notice, that he can't understand R.

Code: http://pastebin.com/X5nrMHK2


i got a little problem with the MediaPlayer..

I want to play the Sound eightsound in the res/raw folder when pressing on the Button eight..

Here is my Code:

package com.iklikla.eightgame;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
Button eight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    eight.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            MediaPlayer mp = MediaPlayer.create(MainActivity .this,R.raw.eightsound);
            mp.start();
        }
    });
}

}

The error is on line 22

MediaPlayer mp = MediaPlayer.create(MainActivity .this,R.raw.eightsound);

It says: Cannot resolve method 'create(com.iklikla.eightgame.MainActivitym ?)

Upvotes: 3

Views: 10325

Answers (4)

Rohan Prasad
Rohan Prasad

Reputation: 161

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.end);
mediaPlayer.start();

Just replace the R.raw.end with your own file.

Upvotes: 2

user3867794
user3867794

Reputation:

The problem here is the new keyword. Remove the new keyword and it will work.

Mediaplayer xyz = Mediaplayer.create(this, R.raw.Filename);

Upvotes: 6

jazeb007
jazeb007

Reputation: 668

simply remove this keyword from the code and it will work.

Upvotes: 0

M410
M410

Reputation: 301

just change MainActivity.this to this !

MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.eightsound);
          mPlayer.start();

Upvotes: 0

Related Questions