我已在档案中搜索过我的问题,但我无法找到任何解决方案。 我是编码新手,并且真的不知道为什么我的按钮接下来不起作用(单击按钮时崩溃)。 这肯定是因为我的arraylist,但为什么呢?我认为我的数组列表大小是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();
}
});
答案 0 :(得分:0)
您的数组实际上包含的元素不超过5个。检查就好
if (data.equals(getString(R.string.category_hiphop)))
不允许您同时添加所有元素。您的数组同时只包含5首歌曲的一个子集。