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 7082265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:01:36+00:00 2026-05-28T07:01:36+00:00

So what I am trying to create is an activity that displays an image

  • 0

So what I am trying to create is an activity that displays an image button. The background for the image button points to an xml in the drawable folder to show the different pictures for on focus and click. That all works fine. I have music in my main activity that is set to loop. By default the image button is set to say Music On. What I want to happen is when the button is clicked the main sound will pause and the button background will change to a different xml drawable layout that says Music Off. When it is clicked again the music will resume where it left off and again switch back to Music On.

One problem I am having is pausing the main sound. Since I’m new to android can a media player variable I reference in my main activity be changed in a different activity? Also, in my options activity I have two if statements under the on click for the image button to check whether the sound is playing or isn’t and then will either pause or resume the music depending on which one it is. I am not sure how to do the second if statement but I have the first one that I think might be right.

Sorry that there are a lot of different things I am trying to do, but I tried to break it down. Also, I am getting force closes as of now when I start the optionsActivity and I will put everything underneath including the main activity because that is where I establish the mainSound. Thanks for any help you can give me.

MainActivity:

package com.crazycastles;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;



public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private MediaPlayer mainSound;


    @Override 


    public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed //Things to Do 
        if(mainSound!= null) { mainSound.pause(); mainSound=null; } finish(); return true; } return super.onKeyDown(keyCode, event); }



    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mainSound = MediaPlayer.create(MainActivity.this, R.raw.mainscreen);
        mainSound.setLooping(true);
        mainSound.start();



        //CREATE BUTTON 1 & SOUND
        final MediaPlayer buttonSound = MediaPlayer.create(
                MainActivity.this, R.raw.swords);

        ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        button1Activity.class));
            }
        }); 

        ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
        multiplayerbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        multiplayerbuttonActivity.class));
            }
        }); 

        ImageButton optionsbutton = (ImageButton) findViewById(R.id.optionsbutton);
        optionsbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        optionsActivity.class));
            }
        }); 

        ImageButton creditbutton = (ImageButton) findViewById(R.id.creditbutton);
        creditbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        creditsActivity.class));
            }
        }); 
        ImageButton exitbutton = (ImageButton) findViewById(R.id.exitbutton);
        exitbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                finish();
                mainSound.stop();
                System.exit(0);

            }
        }); 



        //END OF BUTTON1 & SOUND



        }
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

optionsActivity:

package com.crazycastles;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class optionsActivity extends Activity {
    /** Called when the activity is first created. */
    ImageButton musicbutton, musicbutton2;
    private MediaPlayer mainSound;
    final MediaPlayer buttonSound = MediaPlayer.create(
            optionsActivity.this, R.raw.swords);


    @Override

        public void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.options);



            final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
            musicbutton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

            if(mainSound.isPlaying()) {
                musicbutton.setBackgroundResource(R.drawable.musicbutton2);
                buttonSound.start();
                mainSound.pause();
            }
            }
            });








}
}

LogCat:

01-15 16:10:55.059: E/AndroidRuntime(7319): FATAL EXCEPTION: main
01-15 16:10:55.059: E/AndroidRuntime(7319): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.crazycastles/com.crazycastles.optionsActivity}: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.access$2500(ActivityThread.java:129)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.os.Looper.loop(Looper.java:143)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.main(ActivityThread.java:4701)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at dalvik.system.NativeStart.main(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319): Caused by: java.lang.NullPointerException
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.media.MediaPlayer.create(MediaPlayer.java:641)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at com.crazycastles.optionsActivity.<init>(optionsActivity.java:17)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.Class.newInstanceImpl(Native Method)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at java.lang.Class.newInstance(Class.java:1429)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 16:10:55.059: E/AndroidRuntime(7319):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
  • 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-05-28T07:01:36+00:00Added an answer on May 28, 2026 at 7:01 am

    I also have developed a music player in Android

    One problem I am having is pausing the main sound. Since I’m new to
    android can a media player variable I reference in my main activity be
    changed in a different activity?

    I could say it yes, if you declare it as a static Object

    Also, in my options activity I have two if statements under the on
    click for the image button to check whether the sound is playing or
    isn’t and then will either pause or resume the music depending on
    which one it is. I am not sure how to do the second if statement but I
    have the first one that I think might be right.

    I think you have to look at Android Media Player lifecycle, you could re-use your object but there is some conditions : http://developer.android.com/reference/android/media/MediaPlayer.html

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

Sidebar

Related Questions

I'm trying to create and overlay that displays on top of another activity, in
I am trying to create a custom dialog box that displays an image in
Trying to create a background-image slideshow and am getting this error... This is the
So I'm trying to create an activity that passes some values into another activity
I have a ListView that uses different XML files to create Views and make
I have an Android activity that displays a list of log entries (using a
I am trying to write an application that displays the list of running applications.
i'm trying to code an app that displays the current device position on a
I am trying to create a program that uses regular expression to scrape data
I am trying to create an app that will obtain the users location via

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.