我是新来的,但我每天都在这个论坛上阅读。 我正在构建一个基于左侧抽屉的音乐播放器(使用库“简单的侧抽屉”),当我触摸向上按钮(在操作栏上),然后左侧抽屉打开。 在左侧抽屉上有一个列表视图(简单),包含您所有的歌曲。当我点击一个项目然后应该开始播放...但应用程序崩溃。 这是错误
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)
我不知道为什么,这是代码:
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);
}
}
当我尝试播放某些内容时应用崩溃的原因?请帮帮我:(:)
答案 0 :(得分:0)
这是因为您的mAudioPath
成员为空。请务必在致电onItemClick()
之前进行设置。
答案 1 :(得分:0)
除了所有其他的东西之外,onItemClick()方法的NullPointerException可能在您执行时发生:
playSong(mAudioPath[arg2]);
原因是mAudioPath未初始化且为null。为什么呢?
您已将其声明为您的班级Maincu中的字段:
private String[] mAudioPath;
您认为通过调用getAudioList()来初始化它,但在此方法中,您声明:
String[] mAudioPath = new String[count];
它将您的全局变量mAudioPath视为SHADOW,因此您的本地mAudioPath正在初始化,而不是全局,然后用于播放歌曲。要修复,请在getAudioList()中更改:
String[] mAudioPath = new String[count];
为:
mAudioPath = new String[count];