图像按钮单击时播放声音



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

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 而崩溃?

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

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

问题:我检查了你的日志。

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)

实际上,您正在将图像按钮向下转换为按钮,这是不可能的。

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

溶液:相反,请将其向下投射到图像按钮。

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

这是具有正确向下转换的代码。

公共类 主活动扩展活动 {

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); 
        }
    });
}
}

检查一下,如果还有任何问题,请告诉我...!!

最新更新