简体   繁体   中英

Android: Why isn't my button playing its sound effect?

The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.

Sounds.java

package com.andrew.finnandjake;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sounds extends Activity {
private SoundManager mSoundManager;

/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);

    Button b1 = (Button)findViewById(R.id.Button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mSoundManager.playSound(1);


        }
    });
}
public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

        Button b2 = (Button)findViewById(R.id.Button2);
        b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                mSoundManager.playSound(2);
}
});
    }
}

You can't randomly create a method like you're doing with onCreate1 , it's never executed by you or by your Activity .

This is all you need:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   mSoundManager = new SoundManager();
   mSoundManager.initSounds(this);
   mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
   mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

   Button b1 = (Button)findViewById(R.id.Button1);
   b1.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.playSound(1);
      }
   });

   Button b2 = (Button)findViewById(R.id.Button2);
   b2.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mSoundManager.playSound(2);
      }
   });
}

You aren't assigning the onclick listener inside the actual OnCreate. This should work:

package com.andrew.finnandjake;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sounds extends Activity {
private SoundManager mSoundManager;

/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSoundManager = new SoundManager();
    mSoundManager.initSounds(getBaseContext());
    mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
    mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);

    Button b1 = (Button)findViewById(R.id.Button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mSoundManager.playSound(1);


        }
    });

    Button b2 = (Button)findViewById(R.id.Button2);
    b2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSoundManager.playSound(2);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM