Click here to Skip to main content
15,798,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use my sound from the raw file that whenever I click my search button, it will make a little sound. But it returns the following error:
cannot resolve method create in MediaPlayer
Please how do I fix it?

My HomeActivity Class(Where the search button is):
public class HomeActivity extends AppCompatActivity {
    // User current time
    TextView time_field;
    MediaPlayer player;
    ImageView Search;
    EditText textfield;
    ConstraintLayout constraintLayout;
    // For scheduling background image change
    public static int count=0;
    int[] drawable =new int[]{,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
    Timer _t;

    protected void onCreate(Bundle savedInstanceState) {

        time_field = findViewById(;
        Search = findViewById(;
        textfield = findViewById(;

        BottomNavigationView bottomNavigationView = findViewById(;
        final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(;
        assert navHostFragment != null;
        final NavController navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

        Search.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                player = MediaPlayer.create(HomeActivity.this,;

                FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);

                            constraintLayout = findViewById(;
                            _t = new Timer();
                            _t.scheduleAtFixedRate(new TimerTask() {
                                public void run() {
                                    // run on ui thread
                                    runOnUiThread(() -> {
                                        if (count < drawable.length) {

                                            count = (count + 1) % drawable.length;
                            }, 5000, 5000);

What I have tried:

I added:
MediaPlayer player;

player = MediaPlayer.create(HomeActivity.this,;

to use the sound but getting the above error
Updated 22-May-21 2:03am
David Crow 21-May-21 9:29am    
Does the error happen during compile or execution?

It's not related to the error, but you will eventually need to call the start() method.

Your code snippet seems to be missing three closing brackets.

This compiles and runs as expected for me:
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class HomeActivity extends AppCompatActivity
    protected void onCreate(Bundle savedInstanceState)

        ImageView Search = findViewById(;

        Search.setOnClickListener(new View.OnClickListener()
            public void onClick(View v)
                MediaPlayer player = MediaPlayer.create(HomeActivity.this,;
Chinedum Ezeozue 21-May-21 9:39am    
Please are you sure? It's just for button click sound. Not music
Chinedum Ezeozue 21-May-21 9:40am    
It's compile error
Chinedum Ezeozue 22-May-21 7:19am    
It's running now after changing the sound format. Please post this as an answer so that I can accept. Thank you very much
Chinedum Ezeozue 21-May-21 17:02pm    
Now, It displays the following: java.lang.NullPointerException: Attempt to invoke virtual method 'void' on a null object reference
at com.viz.lightweatherforecast.Activity.HomeActivity$1.onClick(

1 solution

Either one or both of your parameters do not match any of the create method overloads. See MediaPlayer  |  Android Developers[^] for correct usage.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900