gpt4 book ai didi

java - 无错误构建,但在 Android Studio 中的音乐播放器中单击时崩溃

转载 作者:行者123 更新时间:2023-12-02 12:23:26 25 4
gpt4 key购买 nike

我是编程新手,被分配编写一个简单的音乐播放器,有幻灯片可以帮助指导,我对它们进行了轻微的修改(不能使用完全相同的变量)。

所以现在它没有错误,但是当我单击播放按钮时它崩溃了,不知道为什么。这是我选择库的页面的代码:

package sg.edu.tp.project1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


import sg.edu.tp.project1.util.AppUtil;

public class myMusic extends AppCompatActivity {
private String[] Musicsong = new String[6];






@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_music);
//store values in array of the first song
Musicsong[0] = "s1001";
Musicsong[1] = "The Way You Look Tonight";
Musicsong[2] = "ed sheeran";
Musicsong[3] = "a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=null";
Musicsong[4] = "4.39";
Musicsong[5] = "music to play";


}
public void sendDataToActiviy(String[] song)
{
//1.create intent and specify destination
Intent intent = new Intent(this, PlayMusicActivity.class);

//2.store song info in intent to send to destination
intent.putExtra("id", song[0]);
intent.putExtra("title",song[1]);
intent.putExtra("artist",song[2]);
intent.putExtra("fileLink",song[3]);
intent.putExtra("coverArt",song[5]);

//3. launch desntination activiy
startActivity(intent);


}
public void handleSelection(View view)
{

// 1. get id of selected song
String resourceId = AppUtil.getResourceId(this, view);

//2. Search for the selected song based on the ID so that
// all infomation of the song can be retreived

String[] selectedSong =searchById(resourceId);
//3.popup to show tittle of song
AppUtil.popMessage(this, "Streaming song:" + selectedSong[1]);

//4. send song data to player screen
sendDataToActiviy(selectedSong);
}
private Object[] songs = {Musicsong};
public String[] searchById(String id) {
//temporary empty array
String[] song = null;
//for loop to get song
for (int index = 0; index < songs.length; index++) {
//3. store each song item to song array.
song = (String[]) songs[index];

//4. match song id to see if its the one i want
if (song[0].equals(id)) {
return song;

}

}
//if song not found in array empty array will be returned
return song;
}

}

玩家所在的部分:

package sg.edu.tp.project1;

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

import java.io.IOException;

import sg.edu.tp.project1.util.AppUtil;

public class PlayMusicActivity extends AppCompatActivity
{

//streaming website
private static final String BASE_URL = "https://p.scdn.co/mp3-preview/";

private String songId ="";
private String title = "";
private String artist = "";
private String fileLink = "";
private String coverArt = "";
private String url = "";

//builtin media player
private MediaPlayer player = null;

//position of song in playback
private int musicPosition = 0;

//button variable to link to play btn
private Button btnPlayPause = null;

private String[] Musicsong = {

"s1001",
"The Way You Look Tonight",
"ed sheeran",
"a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=null",
"4.39",
"music to play"};



private Object[] songs = {Musicsong};


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

btnPlayPause = (Button) findViewById(R.id.btnPlayPause);

retrieveData();

displaySong(title,artist,coverArt);

}
private void retrieveData() {
Bundle songData = this.getIntent().getExtras();

songId = songData.getString("id");
title = songData.getString("title");
artist = songData.getString("artist");
fileLink = songData.getString("fileLink");
coverArt = songData.getString("coverArt");

url = BASE_URL + fileLink;
}
private void displaySong(String title, String artist, String coverArt)
{
//retrieve song title
TextView txtTitle = (TextView) findViewById(R.id.txtSongTitle);

//set text of song title
txtTitle.setText(title);

//retrieve artist
TextView txtArtist = (TextView) findViewById(R.id.txtArtist);

//set text of artist
txtArtist.setText(artist);

//get id of coverart
int imageId = AppUtil.getImageIdFromDrawable(this,coverArt);

//retrieve coverart
ImageView ivCoverArt = (ImageView) findViewById(R.id.imgCoverArt);

ivCoverArt.setImageResource(imageId);

}






public void playOrPauseMusic(View view)
{// start player
player.start();

//2.
btnPlayPause.setText("PAUSE");

//3.set the heading title of played song
setTitle("Now Playing:" + title + "-" + artist);

}


private void preparePlayer() { //create a new player
player = new MediaPlayer();

try {
//set stream type to music
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//set source of music
player.setDataSource(url);
//prepare player for playback
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}


}}

日志:

08-10 16:04:02.478 2567-2567/sg.edu.tp.project1 I/art: Not late-enabling -Xcheck:jni (already on)
08-10 16:04:02.478 2567-2567/sg.edu.tp.project1 W/art: Unexpected CPU variant for X86 using defaults: x86
08-10 16:04:02.535 2567-2567/sg.edu.tp.project1 W/System: ClassLoader referenced unknown path: /data/app/sg.edu.tp.project1-2/lib/x86
08-10 16:04:02.539 2567-2567/sg.edu.tp.project1 I/InstantRun: Starting Instant Run Server for sg.edu.tp.project1
08-10 16:04:02.913 2567-2567/sg.edu.tp.project1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-10 16:04:03.399 2567-2632/sg.edu.tp.project1 I/OpenGLRenderer: Initialized EGL, version 1.4
08-10 16:04:03.400 2567-2632/sg.edu.tp.project1 D/OpenGLRenderer: Swap behavior 1
08-10 16:04:03.400 2567-2632/sg.edu.tp.project1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-10 16:04:03.400 2567-2632/sg.edu.tp.project1 D/OpenGLRenderer: Swap behavior 0
08-10 16:04:03.490 2567-2567/sg.edu.tp.project1 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
08-10 16:04:14.759 2567-2632/sg.edu.tp.project1 D/OpenGLRenderer: endAllActiveAnimators on 0x8d2e9b00 (RippleDrawable) with handle 0x8d2ff830
08-10 16:04:27.207 2567-2570/sg.edu.tp.project1 I/art: Do partial code cache collection, code=29KB, data=27KB
08-10 16:04:27.207 2567-2570/sg.edu.tp.project1 I/art: After code cache collection, code=29KB, data=27KB
08-10 16:04:27.207 2567-2570/sg.edu.tp.project1 I/art: Increasing code cache capacity to 128KB
08-10 16:04:27.463 2567-2632/sg.edu.tp.project1 D/OpenGLRenderer: endAllActiveAnimators on 0x8c313a00 (RippleDrawable) with handle 0x8d2ff420
08-10 16:04:29.695 2567-2567/sg.edu.tp.project1 D/AndroidRuntime: Shutting down VM

崩溃开始:

08-10 16:04:29.696 2567-2567/sg.edu.tp.project1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: sg.edu.tp.project1, PID: 2567
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at sg.edu.tp.project1.PlayMusicActivity.playOrPauseMusic(PlayMusicActivity.java:106)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

最佳答案

异常信息非常清楚:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference 
at sg.edu.tp.project1.PlayMusicActivity.playOrPauseMusic(PlayMusicActivity.java:106)

因此,您的 player 字段为 null,因为您之前从未调用过 PlayMusicActivitypreparePlayer() .

关于java - 无错误构建,但在 Android Studio 中的音乐播放器中单击时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com