我正在创建一个音乐播放器应用。我有两个名为" Tab3Tracks"和" NowPlaying"。在" Tab3Tacks" class,歌曲在ListView中加载。因此,当有人点击一首歌时,它会将该歌曲的位置发送到其他课程,以便其他课程能够播放该歌曲。但是我在点击歌曲时遇到错误。我不知道我在哪里弄错了。在logcat中,我收到错误"无效索引0,大小为0"在NowPlaying类中。我发布了LogCat错误代码。检查我的代码是否有任何错误。提前谢谢。
Tab3Tracks类
public class Tab3Tracks extends ListFragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private ArrayList<String> trackslist = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab3tracks, container, false);
final String trackid = MediaStore.Audio.Media._ID;
final String trackno = MediaStore.Audio.Media.TRACK;
final String trackname = MediaStore.Audio.Media.TITLE;
final String path = MediaStore.Audio.Media.DATA;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = getContext().getContentResolver();
final String[] projection = {trackid,trackno,trackname,path
};
final Cursor cursor = cr.query(uri,projection,null,null,null);
if (cursor!=null){
if(cursor.moveToFirst()){
do{
int trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
trackslist.add(cursor.getString(trackIndex));
}while(cursor.moveToNext());
}
} cursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),R.layout.playlist_item,R.id.songTitle,trackslist
);
setListAdapter(adapter);
return v;
}
public ArrayList<String> getList(){
return trackslist;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
int songIndex = position;
// Starting new intent
Intent in = new Intent(getActivity(), NowPlaying.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
startActivityForResult(in, 100);
getActivity().finish();
}
}
NowPlaying Class
public class NowPlaying extends AppCompatActivity {
public ArrayList<String> songsList = new ArrayList<String>();
public Tab3Tracks tab3tracks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
// Getting all songs list
tab3tracks = new Tab3Tracks();
songsList= tab3tracks.getList();
// By default play first song
playSong(0);
}
/**
* Receiving song index from playlist view
* and play the song
*/
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}
}
/**
* Function to play a song
*
* @param songIndex - index of song
*/
public void playSong(int songIndex) {
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex);
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
LogCat错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.digital.thefearstuff.tfsmusicplayer/com.digital.thefearstuff.tfsmusicplayer.NowPlaying}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.digital.thefearstuff.tfsmusicplayer.NowPlaying.playSong(NowPlaying.java:286)
at com.digital.thefearstuff.tfsmusicplayer.NowPlaying.onCreate(NowPlaying.java:83)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
答案 0 :(得分:1)
首先,请避免询问为什么这段代码不起作用?寻求调试帮助,因为它们往往会得到大量的downvotes并且通常很快被关闭。始终尽量减少并隔离您的问题。 自行调试,并查看How to create a Minimal, Complete, and Verifiable example以了解日后的帖子。
现在提问,playSong(0)
您尝试访问0
位置的元素,但列表为空且没有此类位置,因此IndexOutOfBounds
。
at com.digital.thefearstuff.tfsmusicplayer.NowPlaying.playSong(NowPlaying.java:286)
at com.digital.thefearstuff.tfsmusicplayer.NowPlaying.onCreate(NowPlaying.java:83)
你的songsList
结果是空的。这反过来意味着trackslist
是空的。这本身可能导致cursor
null
并跳过add
- while循环。但是你应该自己调试,因为你只有整个代码。
进行调试:使用else
添加Log
和if (cursor != null){
,例如
if (cursor!=null){
if(cursor.moveToFirst()){
do{
int trackIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
trackslist.add(cursor.getString(trackIndex));
}while(cursor.moveToNext());
}
} else {
Log.d("cursor", "null");
}
cursor.close();
并在logcat中查找。