java - 数组列表位置和媒体播放器按钮下一个错误
问题描述
我在档案中搜索了我的问题,但找不到任何解决方案。我是编码新手,真的不知道为什么我的下一个按钮不起作用(单击按钮时崩溃)。这绝对是因为我的数组列表,但为什么呢?我认为我的数组列表大小是 20,而不是 5。我知道 5 是根据点击的类别显示的歌曲数,但我没有为每个类别创建新的数组列表。有什么帮助吗?
public class listViewOfSongs extends AppCompatActivity {
// Varaibles
public ArrayList<Songs> songs = new ArrayList<>();
String songCategory;
//Needed for getting data from MainActivity
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_list);
//Getting data from main activity
Bundle bundle = getIntent().getExtras();
data = bundle.getString("data");
// Display list of songs based on category chosen
displaySongs();
}
// Method to display list of songs
public void displaySongs() {
// Create a list of songs
songs = new ArrayList<>();
if (data.equals(getString(R.string.category_electronic))) {
// Set category
songCategory = getString(R.string.category_electronic);
// Add songs
songs.add(new Songs("I Don't Cry", "A Virtual Friend", 0));
songs.add(new Songs("Light", "Hazardous",1));
songs.add(new Songs("First", "JekK",2));
songs.add(new Songs("Energy", "Pokki DJ",3));
songs.add(new Songs("Pangea", "Professor Kliq",4));
} else if (data.equals(getString(R.string.category_hiphop))) {
// Set category
songCategory = getString(R.string.category_hiphop);
// Add songs
songs.add(new Songs("Drop", "Z-Major",5));
songs.add(new Songs("You Got Me Rocking", "Ed Napioli",6));
songs.add(new Songs("Summertimewav", "Emcee Lynx",7));
songs.add(new Songs("Let It Go", "Kidd Young ft. Michael John",8));
songs.add(new Songs("Deep Club Music", "Sam Valley",9));
} else if (data.equals(getString(R.string.category_house))) {
// Set category
songCategory = getString(R.string.category_house);
// Add songs
songs.add(new Songs("Disc Over", "Becaysi",10));
songs.add(new Songs("Light", "Hazardous",11));
songs.add(new Songs("Seasons", "NumBack",12));
songs.add(new Songs("Housing", "RasL",13));
songs.add(new Songs("Hypnotize Me", "The Artisans Beats",14));
} else if (data.equals(getString(R.string.category_latin))) {
// Set category
songCategory = getString(R.string.category_latin);
// Add songs
songs.add(new Songs("Bony", "T.B.T ft. Bufalo El Fuete ",15));
songs.add(new Songs("Solo Amigos", "Edward Jay",16));
songs.add(new Songs("Me Canse", "Harveys",17));
songs.add(new Songs("Sola", "Mackenzie La Fuerza",18));
songs.add(new Songs("Fallin Down", "The Madpix Project",19));
}
// Create an {@link SongsAdapter}, whose data source is a list of {@link Songs}. The
// adapter knows how to create list items for each item in the list.
SongsAdapter adapter = new SongsAdapter(this, 0, songs);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// songs_list.xml layout file.
final ListView listView = findViewById(R.id.list);
// Make the {@link ListView} use the {@link SongsAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Songs} in the list.
listView.setAdapter(adapter);
// Set OnClickListener on ListView to identify the item on ListView clicked by user
// song on the ListView item clicked is passed on to player activity
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Getting list item index
Intent intent = new Intent(getApplicationContext(), Player.class);
intent.putParcelableArrayListExtra("song", songs);
intent.putExtra("title", songs.get(position).getSongTitle());
intent.putExtra("artist", songs.get(position).getSongArtist());
intent.putExtra("pos", songs.get(position).getRawSong());
intent.putExtra("index", songs.get(position));
intent.putExtra("data", data);
// Closing PlayListView and opening new activity
startActivity(intent);
}
});
}
我的按钮下一个方法:
btnNext = findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
PartyMp.stop();
PartyMp.reset();
pos = pos + 1;
PartyMp = MediaPlayer.create(getApplicationContext(), rawF[pos]);
titleOfSong.setText(String.valueOf(songs.get(pos).getSongTitle()));
artistOfSong.setText(String.valueOf(songs.get(pos).getSongArtist()));
PartyMp.start();
}
});
解决方案
您的数组实际上包含不超过 5 个元素。诸如
if (data.equals(getString(R.string.category_hiphop)))
不允许您同时添加所有元素之类的检查。您的数组确实同时只包含 5 首歌曲的一个子集。
推荐阅读
- arrays - 如何从MongoDB中的数组中删除特定的元素
- google-chrome - 在chrome中调试vue时断点链接位置不正确
- javascript - 使用 React 的背景位置内联样式
- hadoop - YARN 应用程序日志在发送到 S3 之前存储在 EMR 中的什么位置
- python - tensorflow如何解决one_hot和sign之间的dtype冲突
- r - 任务计划 R 脚本每分钟运行一次
- python - 为什么此命令在我的终端上有效,但在我的代码中无效?
- javascript - Javascript es6 类继承
- mysql - 您可以做些什么来加快数据库数据加载速度
- javascript - 使用带有变量的 JavaScript Math 对象