android studio应用程序持续崩溃,没有任何错误

hkmswyz6  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(211)

我制作了一个android studio应用程序,当你点击一个按钮时,它会播放mp3文件。突然它开始崩溃。我已经亲自检查了代码,我无法发现任何错误。我也尝试重新安装仿真器,并使用物理android设备进行连接,但仍然出现同样的问题。以下是java文件:

public class MainActivity extends AppCompatActivity {

    private Button yes;
    private Button no;
    private ImageButton iwant;
    private ImageButton food;
    private ImageButton restroom;
    private ImageButton water;
    private ImageButton playground;
    private ImageButton rice;
    private ImageButton school;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        yes = findViewById(R.id.yes);
        no = findViewById(R.id.no);
        iwant = findViewById(R.id.iwant);
        food = findViewById(R.id.food);
        restroom = findViewById(R.id.restroom);
        water = findViewById(R.id.water);
        playground = findViewById(R.id.playground);
        rice = findViewById(R.id.rice);
        school = findViewById(R.id.school);

        final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.yes);
        final MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.no);
        final MediaPlayer mediaPlayer3 = MediaPlayer.create(this,R.raw.iwant);
        final MediaPlayer mediaPlayer4 = MediaPlayer.create(this,R.raw.food);
        final MediaPlayer mediaPlayer5 = MediaPlayer.create(this,R.raw.restroom);
        final MediaPlayer mediaPlayer6 = MediaPlayer.create(this,R.raw.water);
        final MediaPlayer mediaPlayer7 = MediaPlayer.create(this,R.raw.playground);
        final MediaPlayer mediaPlayer8 = MediaPlayer.create(this,R.raw.rice);
        final MediaPlayer mediaPlayer9 = MediaPlayer.create(this,R.raw.school);

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer.start();
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer2.start();
            }
        });
        iwant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer3.start();
            }
        });
        food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer4.start();
            }
        });
        restroom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer5.start();
            }
        });
        water.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer6.start();
            }
        });
        playground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer7.start();
            }
        });
        rice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer8.start();
            }
        });
        school.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mediaPlayer9.start();
            }
        });
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题