我想创建一个播放列表,以便当用户点击歌曲时播放,当他点击下一首歌曲时,正在播放的歌曲将停止并恢复下一首歌曲。此外,我已经放置了一个停止按钮,也停止正在播放的歌曲。现在,这是我编写以下代码块时的问题,我的程序崩溃,我的错误日志显示java.lang.NullPointerException -
public void playSong(int res)
{
if(mp.isPlaying()) {
mp.reset();
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
else {
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
}
但是,如果我将代码更改为以下代码,它可以正常工作。我看到mp.reset()是个问题。但是我不知道如何修复它,因为没有它,当我点击一首歌并且已经播放的歌曲时,两首歌曲同时播放.-
public void playSong(int res)
{
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
//以下是我的完整代码。请帮我修复bug。谢谢 -
package com.example.user.musicp;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
Button b;
private MediaPlayer mp;
private ListView v;
private int [] resid = {R.raw.k,R.raw.u};
private String [] name = {"k","u"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
v.setAdapter(adapter);
b = (Button) findViewById(R.id.StopButton);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// mp.stop();
// mp.release();
onDestroy();
}
});
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
playSong(i);
// mp = MediaPlayer.create(MainActivity.this, resid[i]);
// mp.start();
}
});
}
public void playSong(int res)
{
if(mp.isPlaying()) {
mp.reset(); //this is where the issue begins
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
else {
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
参考文献: - http://www.geeks.gallery/how-to-play-mp3-file-from-raw-folder-in-a-listview-android/
在一些用户提出一些有价值的建议之后,我编辑了我的代码,并且能够修复错误。但是当我点击一首歌并且有一首已经播放的歌曲时,我希望停止播放上一首歌并播放新歌,但不幸的是两首歌都在播放。我尝试了一切可能的方法来解决它,但我可能在某个地方犯了错误。这是我编辑的代码,当我实现以下内容时,我的程序崩溃了。让我帮忙 -
public void playSong(int res)
{
boolean isplaying = mp.isPlaying();
if(isplaying)
{
mp.stop();
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
else
{
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
}
以下是完整的工作代码,以防万一将来陷入困境。非常感谢StackOverflow用户帮助我。干杯!
package com.example.user.musicp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.io.IOException;
public class MainActivity extends Activity {
Button b,c;
private MediaPlayer mp;
private ListView v;
private int [] resid = {R.raw.k,R.raw.u};
private String [] name = {"k","u"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
v = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
v.setAdapter(adapter);
b = (Button) findViewById(R.id.StopButton);
c = (Button) findViewById(R.id.next);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.stop();
}
});
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent("com.example.user.musicp.SPLASH");
startActivity(in);
}
});
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
playSong(i);
// mp = MediaPlayer.create(MainActivity.this, resid[i]);
// mp.start();
}
});
}
public void playSong(int res)
{
mp.reset();// stops any current playing song
mp = MediaPlayer.create(getApplicationContext(), resid[res]);// create's
mp.start(); // starting mediaplayer
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
if(mp.isPlaying()) { // mp only declared (still null)
mp.reset();
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start(); }
else {
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
mp.start();
}
我想在if
语句mp
的那一刻才宣布。
这就是为什么你不能mp.isPlaying()
因为mp
仍然是null
的原因。
这应该解释为什么在这种情况下它可以正常工作
public void playSong(int res)
{
// here mp is still null
mp = MediaPlayer.create(getApplicationContext(), resid[res]); // From here on we can use mp
mp.start();
}
这里没有在初始化之前使用mp,因此没有空指针异常。
编辑:在playSong
方法中创建媒体播放器有点奇怪。这意味着每次调用playSong
时mp
都会被重新分配。一个更好的地方可能是在类的构造函数中。这样做可以确保mp
在null
被调用时不会playSong
。
编辑2:您仍在mp
重新分配playSong
。
我建议这样做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = MediaPlayer.create(getApplicationContext(), resid[res]);
// ...
}
我会像这样实施playSong
:
public void playSong(int songIndex)
{
if(mp.isPlaying()) {
mp.pause(); // Pause the current track
}
mp.selectTrack(songIndex); // Select the requested track
mp.start();
}