i'm new here but I read every day questions on this forum. I'm building a music player based on a side drawer on the left (using the library "simple side drawer), when I touch the up button (on the action bar) then the left drawer open. On the left drawer there is a listview (simple) with all your's songs. When I tap on a item then that should start playing...but the app crash. This is the error
12-08 14:33:58.760: E/AndroidRuntime(12420): FATAL EXCEPTION: main
12-08 14:33:58.760: E/AndroidRuntime(12420): java.lang.NullPointerException
12-08 14:33:58.760: E/AndroidRuntime(12420): at com.xand.holomusicplayer.Maincu$1.onItemClick(Maincu.java:58)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.widget.AdapterView.performItemClick(AdapterView.java:297)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.widget.AbsListView.performItemClick(AbsListView.java:1123)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2931)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.widget.AbsListView$2.run(AbsListView.java:3616)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.os.Handler.handleCallback(Handler.java:730)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.os.Looper.loop(Looper.java:137)
12-08 14:33:58.760: E/AndroidRuntime(12420): at android.app.ActivityThread.main(ActivityThread.java:5287)
12-08 14:33:58.760: E/AndroidRuntime(12420): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 14:33:58.760: E/AndroidRuntime(12420): at java.lang.reflect.Method.invoke(Method.java:525)
12-08 14:33:58.760: E/AndroidRuntime(12420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
12-08 14:33:58.760: E/AndroidRuntime(12420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
12-08 14:33:58.760: E/AndroidRuntime(12420): at dalvik.system.NativeStart.main(Native Method)
I don't know why, this is the code:
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
private SimpleSideDrawer mSlidingMenu;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
getActionBar().setDisplayHomeAsUpEnabled(true);
mSlidingMenu = new SimpleSideDrawer(this);
mSlidingMenu.setLeftBehindContentView(R.layout.playlist);
list = (ListView) findViewById (R.id.listView1);
mMediaPlayer = new MediaPlayer();
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mMusicList);
list.setAdapter(mAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
String[] mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
mSlidingMenu.toggleLeftDrawer();
return true;
}
return super.onOptionsItemSelected(item);
}
}
why the app crash when I try to play something ? Please Help me :( :)
This is because your mAudioPath
member is null. Be sure to set it before calling onItemClick()
.
All other things aside, your NullPointerException from onItemClick() method happens probably when you do:
playSong(mAudioPath[arg2]);
The reason for this is that mAudioPath is not initialised and null. Why?
You have declared it as a field in your class Maincu as:
private String[] mAudioPath;
You think you are initialising it by calling getAudioList(), but inside this method, you are declaring:
String[] mAudioPath = new String[count];
which SHADOWS your global variable mAudioPath, so your local mAudioPath is being initialised, instead of global, which is then used to play the song. To fix, in your getAudioList(), change:
String[] mAudioPath = new String[count];
to:
mAudioPath = new String[count];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.