Android Media player创建方法似乎对我的项目不起作用,导致我的应用程序在启动第一个活动时崩溃。有人帮帮我吗
持有媒体播放器创建方法的Util类: {Android Media player创建方法似乎对我的项目不起作用,导致我的应用程序在启动第一个活动时崩溃。有人帮帮我吗,android,Android,持有媒体播放器创建方法的Util类: { 私有静态数组列出所有歌曲; 私有静态ArrayList收藏夹歌曲; 私有静态int id=0; 语境; 公共Util(上下文){ this.context=上下文; } 公共Util(){ 如果(所有歌曲==null){ allSongs=newarraylist(); 初始化所有歌曲(); } if(收藏夹歌曲==null){ 收藏夹歌曲=新的ArrayList(); } } 公共静态数组列表getAllSongs(){ 返回所有歌曲; } 公共静态A
私有静态数组列出所有歌曲;
私有静态ArrayList收藏夹歌曲;
私有静态int id=0;
语境;
公共Util(上下文){
this.context=上下文;
}
公共Util(){
如果(所有歌曲==null){
allSongs=newarraylist();
初始化所有歌曲();
}
if(收藏夹歌曲==null){
收藏夹歌曲=新的ArrayList();
}
}
公共静态数组列表getAllSongs(){
返回所有歌曲;
}
公共静态ArrayList GetFavoriteShongs(){
返回喜爱的歌曲;
}
公共无效添加到AVSONGS(Song Song){
收藏歌曲。添加(歌曲);
}
从Favsongs(歌曲)中删除公共空白{
收藏夹歌曲。删除(歌曲);
}
私有无效初始化AllSongs(){
int songNumber=0;
字符串songTitle=“”;
弦乐歌词=”;
MediaPlayer音频播放器;
id++;
宋数=1;
songTitle=“示例歌曲标题”;
Songlyrs=“示例歌词示例歌词\n”+
“歌词示例歌词示例\n”+
“歌词示例歌词示例\n”+
“歌词样本”;
audioPlayer=新媒体播放器();
audioPlayer=MediaPlayer.create(context.getApplicationContext(),R.raw.audio);
添加(新歌(id、歌曲编号、歌曲标题、歌词、音频播放器));
id++;
宋数=2;
songTitle=“示例歌曲标题”;
Songlyrs=“示例歌词示例歌词\n”+
“歌词示例歌词示例\n”+
“歌词示例歌词示例\n”+
“歌词样本”;
audioPlayer=新媒体播放器();
audioPlayer=MediaPlayer.create(context.getApplicationContext(),R.raw.audi);
添加(新歌(id、歌曲编号、歌曲标题、歌词、音频播放器));
}
}
主要活动(部分活动):
recyclerView=findviewbyd(R.id.songsRecView);
SongRecViewAdapter=新的SongRecViewAdapter(此);
recyclerView.setAdapter(适配器);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
Util=newutil();
ArrayList allSongs=新建ArrayList();
allSongs=util.getAllSongs();
套曲(所有歌曲);
日志:
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.Context android.content.Context.getApplicationContext()”
在com.example.mrfpc.Util.initializingAllSongs上(Util.java:62)
在com.example.mrfpc.Util.(Util.java:22)
在com.example.mrfpc.MainActivity.onCreate(MainActivity.java:36)上,发生这种情况是因为您没有在构造函数中初始化
上下文,而是在活动中初始化
在第二个构造器中添加Context
,并删除拥有它的构造器,在那里初始化它,它将不再抛出它。而不是为每首歌曲创建mediaPlayer
的实例,在MainActivity
中声明一个实例,并使用Title
播放歌曲。感谢您回答Swapnil Padaya,但主要关注点是引发异常的create方法。我提供的logcat包含指向问题来源的链接,但我无法解决它们。Util Util=new Util()
创建实例时,请尝试在util中传递MainActivity
的ApplicationContext()
。创建(/*MainAcitvity context*/,raw file)
试试这个方法。Util(Context ctx)@Phillips如果有效,请将其标记为已接受:)
private static ArrayList<Song> allSongs;
private static ArrayList<Song> favouriteSongs;
private static int id = 0;
Context context;
public Util(Context context) {
this.context = context;
}
public Util() {
if (allSongs == null){
allSongs = new ArrayList<>();
initializingAllSongs();
}
if (favouriteSongs == null){
favouriteSongs = new ArrayList<>();
}
}
public static ArrayList<Song> getAllSongs() {
return allSongs;
}
public static ArrayList<Song> getFavouriteSongs() {
return favouriteSongs;
}
public void addToFavSongs(Song song){
favouriteSongs.add(song);
}
public void removeFromFavSongs(Song song){
favouriteSongs.remove(song);
}
private void initializingAllSongs(){
int songNumber = 0;
String songTitle = "";
String songLyrics = " ";
MediaPlayer audioPlayer ;
id++;
songNumber = 1;
songTitle = "Sample song Title ";
songLyrics = "Sample lyrics Sample lyrics \n" +
" Sample lyrics Sample lyrics \n" +
"Sample lyrics Sample lyrics \n" +
" Sample lyrics Sample lyrics";
audioPlayer = new MediaPlayer();
audioPlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.audio);
allSongs.add(new Song(id, songNumber, songTitle, songLyrics, audioPlayer ));
id++;
songNumber = 2;
songTitle = "Sample song Title ";
songLyrics = "Sample lyrics Sample lyrics \n" +
" Sample lyrics Sample lyrics \n" +
"Sample lyrics Sample lyrics \n" +
" Sample lyrics Sample lyrics";
audioPlayer = new MediaPlayer();
audioPlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.audi);
allSongs.add(new Song(id, songNumber, songTitle, songLyrics, audioPlayer ));
}
recyclerView = findViewById(R.id.songsRecView);
SongRecViewAdapter adapter = new SongRecViewAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Util util = new Util();
ArrayList<Song> allSongs = new ArrayList<>();
allSongs = util.getAllSongs();
adapter.setSongs(allSongs);