图像按钮上的声音播放单击

时间:2014-01-17 08:20:15

标签: android imagebutton

我想在点击图片按钮时播放声音。我尝试播放声音,但它在启动时崩溃应用程序这是我的代码。

public class MainActivity extends Activity {

    ImageButton imageButton;
    Animation performAnimation1;
    ImageView androidImageView;
    MediaPlayer mp;
    Button touch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
        performAnimation1 = AnimationUtils.loadAnimation(this, R.layout.animation1);
        performAnimation1.setRepeatCount(4);
        androidImageView = (ImageView)findViewById(R.id.androidImageView);
        androidImageView.startAnimation(performAnimation1);

        mp = MediaPlayer.create(this, R.raw.click);
        touch = (Button)this.findViewById(R.id.button1);
        touch.setOnClickListener(new View.OnClickListener() {

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

                mp.start();
            }
        });
        }

    public void addListenerOnButton() {
        imageButton = (ImageButton) findViewById(R.id.button1);
        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(MainActivity.this, Numbers.class);
                    startActivity(intent); 
            }
        });
    }

}

由于我在代码中使用onClickListener twise而导致崩溃吗?

2 个答案:

答案 0 :(得分:1)

您需要设置R.anim而不是R.layout

 performAnimation1 = AnimationUtils.loadAnimation(this, R.anim.animation1);

答案 1 :(得分:0)

问题:我检查了你的logcat。

Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 
at com.android.learning_numbers.MainActivity.onCreate(MainActivity.java:33)

实际上你正在将ImageButton向下转换为Button,这是不可能的。

touch = (Button)this.findViewById(R.id.button1);

<强>解决方案: 相反,请将其转发给ImageButton。

touch = (ImageButton)this.findViewById(R.id.button1);

这是您正确向下转发的代码。

公共类MainActivity扩展了Activity {

ImageButton imageButton;
Animation performAnimation1;
ImageView androidImageView;
MediaPlayer mp;
ImageButton touch;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
    performAnimation1 = AnimationUtils.loadAnimation(this, R.layout.animation1);
    performAnimation1.setRepeatCount(4);
    androidImageView = (ImageView)findViewById(R.id.androidImageView);
    androidImageView.startAnimation(performAnimation1);

    mp = MediaPlayer.create(this, R.raw.click);
    touch = (ImageButton)this.findViewById(R.id.button1);
    touch.setOnClickListener(new View.OnClickListener() {

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

            mp.start();
        }
    });
    }

public void addListenerOnButton() {
    imageButton = (ImageButton) findViewById(R.id.button1);
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MainActivity.this, Numbers.class);
                startActivity(intent); 
        }
    });
}

}

检查并告诉我你是否还有任何问题...... !!!