我的应用程序成功构建,没有错误,但是在外部设备上启动时崩溃

时间:2019-07-19 14:55:17

标签: java android

我试图制作一个具有播放,暂停和停止选项的简单音乐播放器应用程序,但它崩溃并在setContentView(R.layout.activity_main)中显示错误; 我不明白现在该怎么办,因为网络上没有特定的解决方案

主要活动代码:

package com.example.musicplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button play,pause,stop;
    MediaPlayer mediaPlayer;
    int pauseCurrentPosition;

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

        play = (Button) findViewById(R.id.btn_play);
        pause = (Button) findViewById(R.id.btn_pause);
        stop = (Button) findViewById(R.id.btn_stop);

        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.btn_play:
                if(mediaPlayer==null){
                mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.despacito);
                mediaPlayer.start();}

                else if (!mediaPlayer.isPlaying()){
                    mediaPlayer.seekTo(pauseCurrentPosition);
                    mediaPlayer.start();
                }
                break;

            case R.id.btn_pause:
                if(mediaPlayer!=null){
                    mediaPlayer.pause();
                    pauseCurrentPosition=mediaPlayer.getCurrentPosition();
                }
                break;

            case R.id.btn_stop:
                if(mediaPlayer!=null) {
                    mediaPlayer.stop();
                    mediaPlayer = null;
                }
                break;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/musicplayer"
    tools:context="com.example.musicplayer.MainActivity">


    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="85dp"
        android:src="@drawable/music"
        app:civ_border_color="#FF000000"
        app:civ_border_width="2dp"
        android:layout_marginStart="90dp" />

    <Button
        android:id="@+id/btn_play"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_below="@+id/profile_image"
        android:layout_marginLeft="145dp"
        android:layout_marginTop="87dp"
        android:background="@drawable/play_btn"
        android:layout_marginStart="145dp" />

    <Button
        android:id="@+id/btn_pause"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignBottom="@+id/btn_play"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="1dp"
        android:background="@drawable/pause_icon"
        android:layout_marginRight="20dp" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignEnd="@+id/profile_image"
        android:layout_alignRight="@+id/profile_image"
        android:layout_alignBottom="@+id/btn_play"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginEnd="-47dp"
        android:layout_marginRight="-47dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/stop_btn" />





</RelativeLayout>

崩溃的Logcat错误消息:-

  

在com.example.musicplayer.MainActivity.onCreate(MainActivity.java:18)

0 个答案:

没有答案