Click here to Skip to main content
15,301,401 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.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
    Timer _t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        time_field = findViewById(R.id.textView9);
        Search = findViewById(R.id.imageView4);
        textfield = findViewById(R.id.textfield);

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

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

                player = MediaPlayer.create(HomeActivity.this, R.raw.search);

                getWeatherData(textfield.getText().toString().trim());
                FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);
                firstFragment.getWeatherData(textfield.getText().toString().trim());

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

                                            constraintLayout.setBackgroundResource(drawable[count]);
                                            count = (count + 1) % drawable.length;
                                        }
                                    });
                                }
                            }, 5000, 5000);
                        }


What I have tried:

I added:
MediaPlayer player;


and
player = MediaPlayer.create(HomeActivity.this, R.raw.search);


to use the sound but getting the above error
Posted
Updated 22-May-21 1:03am
v3
Comments
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.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        ImageView Search = findViewById(R.id.imageView4);

        Search.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                MediaPlayer player = MediaPlayer.create(HomeActivity.this, R.raw.search);
                player.start();
            }
        });
    }
}
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 android.media.MediaPlayer.start()' on a null object reference
at com.viz.lightweatherforecast.Activity.HomeActivity$1.onClick(HomeActivity.java:70)

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.
   

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