Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:40:33+00:00 2026-06-15T08:40:33+00:00

I have a sound that I want to be played when a button clicked.

  • 0

I have a sound that I want to be played when a button clicked. The same sound over many activities. So I thought it could be a good idea to create a Utility class that will have the method for playing the sound and I will call it from various activities, instead of creating a MediaPlayer variable in all the other activities. So I created this Utility class:

public class Utilities extends Activity {

public MediaPlayer mpPositive;
public MediaPlayer mpNegative;

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

    mpPositive = MediaPlayer.create(this, R.raw.click_positive);
    mpNegative = MediaPlayer.create(this, R.raw.click_negative);
}


public void playPositive() {
    mpPositive.start();
}

}

In my activity I imported this file. And then I tried to use it like that:

public class ActivityListCategories extends ListActivity implements
     OnClickListener {
        private Utilities myUtilities;
        ...rest of the code...

    public void onCreate(Bundle savedInstanceState) {

    myUtilities = new Utilities();
            ...rest of the code...
            }
    }

public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnAdd:
        myUtilities.playPositive();
                    ...rest of the code...
}

But when I click the button – my app crashes. What am I doing wrong and how do I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:40:34+00:00Added an answer on June 15, 2026 at 8:40 am

    Your code crashes because the onCreate() method of Utilities is never called as you don’t use it as a normal Activity, and it doesn’t follow the Activity life cycle. Due to this, your this reference will always be null.

    Instead, try making it a normal Java class, something like:

    public class Utilities extends Activity {
    
        public MediaPlayer mpPositive;
        public MediaPlayer mpNegative;
    
        public Utilities(Context context) {
            mpPositive = MediaPlayer.create(context, R.raw.click_positive);
            mpNegative = MediaPlayer.create(context, R.raw.click_negative);
        }
    
    
        public void playPositive() {
            mpPositive.start();
        }
    
    }
    

    And then create an object as follows:

    Utilities utils = new Utilities(this);
    

    Make sure this line is in onCreate() or after of your main Activity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that plays a sound and i want to prevent users
I have a sound cue that I want played whenever the player does a
I have app in which i have recorded sound files i want that i
i have a button that plays a sound. When i push the button multiple
In my application, I have put a button that plays its own sound by
I want to play a sound everytime that a button is pressed. The problem
I'm attempting to have a button that mutes sound in app which now works.
There are other questions on here that sound similar but are not. I have
For the needs of my game I need to have 2D sound. That means,
I have a question that may sound odd, but being somewhat of a newbie,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.