Muralitharan
Muralitharan

Reputation: 27

Error message: can not resolve method Create(anonymous android.view.View.OnClickListener,int)

I wish to play a specific music file from raw folder in Android Studio, on click of a specific button. However I get the following response:

Error message -can not resolve method ' Create(anonymous android.view.View.OnClickListener,int)

My code is as below:

Button t1 = (Button) findViewById(R.id.choose_Tamil1);
t1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        mediaPlayer =MediaPlayer.create(this,R.raw.melam);
    }
});

Upvotes: 2

Views: 2547

Answers (2)

Khloud Abbas
Khloud Abbas

Reputation: 11

TRY THIS

mediaPlayer =MediaPlayer.create(getContext(),R.raw.melam);

Upvotes: 1

Sayan Sil
Sayan Sil

Reputation: 6095

Try this code:

Button t1 = (Button) findViewById(R.id.choose_Tamil1);
t1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
        mediaPlayer =MediaPlayer.create(CLASSNAME.this,R.raw.melam); 
    } 
});

And replace CLASSNAME with the name of the current class in which you are working in.

The error was because this keyword was referring to the OnClickListener class in general, but it should have referred to the custom OnClickListener class you wrote within your current class.

Hence the change.

Upvotes: 1

Related Questions