每次尝试播放歌曲时都会出现以下错误,我在主要活动中添加代码时成功播放了歌曲,但是当尝试在其他活动中播放应用程序崩溃时我只是想知道为什么会这样发生以及如何解决问题。
错误:
E/MediaPlayer: error (1, -2147483648)
D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1158)
at android.media.MediaPlayer.create(MediaPlayer.java:944)
at android.media.MediaPlayer.create(MediaPlayer.java:915)
at com.example.android.phaseup.AviationSongActivity.onCreate(AviationSongActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.phaseup, PID: 1644
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.example.android.phaseup.AviationSongActivity$1.onClick(AviationSongActivity.java:36)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
代码MainActivity.java:
package com.example.android.phaseup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call BlueBook Activity
TextView blueBook = (TextView) findViewById(R.id.blue_book);
//Set a click listener in that View
blueBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent blueBookIntent = new Intent(MainActivity.this, BlueBookActivity.class);
startActivity(blueBookIntent);
}
});
//Call Aviation Song Activity
TextView aviationSong = (TextView) findViewById(R.id.aviation_songs);
//Set a click listener in that View
aviationSong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent aviationIntent = new Intent(MainActivity.this, AviationSongActivity.class);
startActivity(aviationIntent);
}
});
//Call Soldier's Creed Activity
TextView soldierCreed = (TextView) findViewById(R.id.soldier_creed);
//Set a click listener in that View
soldierCreed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent soldierCreedIntent = new Intent(MainActivity.this, SoldierCreedActivity.class);
startActivity(soldierCreedIntent);
}
});
//Call Room Inspection Activity
TextView roomInspection = (TextView) findViewById(R.id.room_inspection);
//Set a click listener in that View
roomInspection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent roomInspectionIntent = new Intent(MainActivity.this, RoomInspectionActivity.class);
startActivity(roomInspectionIntent);
}
});
//Call Wall Locker Inspection Activity
TextView wallLocker = (TextView) findViewById(R.id.wall_Locker_inspection);
//Set a click listener in that View
wallLocker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent wallLockerIntent = new Intent(MainActivity.this, WallLockerActivity.class);
startActivity(wallLockerIntent);
}
});
//Call ASU Inspection Activity
TextView asuInspection = (TextView) findViewById(R.id.asu_inspection);
//Set a click listener in that View
asuInspection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent asuIntent = new Intent(MainActivity.this, ASUActivity.class);
startActivity(asuIntent);
}
});
//Feedback button.
TextView sendFeedBack = (TextView) findViewById(R.id.send_feedback);
sendFeedBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendFeedbackIntent = new Intent(Intent.ACTION_SENDTO);
sendFeedbackIntent.setData(Uri.parse("mailto: algenisromero@gmail.com"));
sendFeedbackIntent.putExtra(Intent.EXTRA_SUBJECT, "Phase UP Support.");
if (sendFeedbackIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendFeedbackIntent);
}
}
});
}
}
Code AviationSongActivity.java:
package com.example.android.phaseup;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AviationSongActivity extends AppCompatActivity {
//Media Player variable.
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aviation_song);
//Create and Upload the MP3 file.
mMediaPlayer = MediaPlayer.create(this, R.raw.aviationsong);
//The aviation song text show in the screen.
TextView textViewAviation = (TextView) findViewById(R.id.textview_aviation);
textViewAviation.setText("High above the best, high above the best \n\n" +
"We are Army Aviation USA, \n\n" +
"proud and strong We meet the test \n\n" +
"Skies filled with thunder \n\n" +
"Wearing silver wings upon our chest \n\n" +
"We meet the needs of Ground Command \n\n" +
"As we aid the Nation's quest \n\n" +
"Army Aviation, flying high above the best!");
//Listener to play sound when user touched.
Button playButton = (Button) findViewById(R.id.play_sound);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();
}
});
}
}
代码activity_aviation_song.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/activity_aviation_song"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.phaseup.AviationSongActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="@color/colorPrimaryDark">
<Button
android:id="@+id/play_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="PLAY"
android:textSize="56sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:background="@null"/>
</RelativeLayout>
<TextView
android:id="@+id/textview_aviation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:gravity="center"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
您可以在我的github帐户中找到该应用的完整代码 PhaseUp App
感谢您的帮助!