Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在图像上播放声音按钮单击_Android_Imagebutton - Fatal编程技术网

Android 在图像上播放声音按钮单击

Android 在图像上播放声音按钮单击,android,imagebutton,Android,Imagebutton,我想在这里播放声音时,点击图像按钮。我试图播放声音,但它在应用程序启动时崩溃。这是我的代码 public class MainActivity extends Activity { ImageButton imageButton; Animation performAnimation1; ImageView androidImageView; MediaPlayer mp; Button touch; @Override public

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

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吗???

您需要设置
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)
实际上,您正在向下转换ImageButton到Button,这是不可能的

touch = (Button)this.findViewById(R.id.button1);
解决方案: 相反,请将其向下投射到ImageButton

touch = (ImageButton)this.findViewById(R.id.button1);
这是正确向下投射的代码。

公共类MainActivity扩展了活动{

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

}

查看它并让我知道您是否还有任何问题…!!!

@vishesh chandra 01-17 03:33:24.841:E/AndroidRuntime(1798):在android.app.Activity.performCreate(Activity.java:5133)01-17 03:33:24.841:E/AndroidRuntime(1798):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)01-17 03:33:24.841:E/AndroidRuntime(1798):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)@SuRajPrince你能编辑你的问题并发布完整的日志吗???@visheshchandra这是日志()@visheshchandra声音正在播放,但不会像以前那样进入我的下一个活动。声音正在播放,但不会像以前那样进入我的下一个活动