package com.fengchuxiaodai.musicplayer; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fengchuxiaodai.bean.Music; import com.fengchuxiaodai.music.db.MyMusicDao; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private final String TAG = "MainActivity"; private Message msg; protected static final int SUCCESSDELETE = 0; protected static final int ERRORDELETE = 1; protected static final int ITEMDELETE = 2; protected static final int FINDMUSIC = 3; private static final int SDCARDERROR = 4; private ListView lv_main_listview; private List<Music> musicList; private MediaPlayer mediaPlayer; private MyMusicDao myMusicDao; private Music music; private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SUCCESSDELETE: Toast.makeText(MainActivity.this, msg.obj + "", 0).show(); break; case ERRORDELETE: Toast.makeText(MainActivity.this, msg.obj + "", 0).show(); break; case ITEMDELETE: Toast.makeText(MainActivity.this, msg.obj + "", 0).show(); break; case SDCARDERROR: Toast.makeText(MainActivity.this, msg.obj + "", 0).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_main_listview = (ListView) findViewById(R.id.lv_main_listview); msg = Message.obtain(); myMusicDao = new MyMusicDao(MainActivity.this); // 每次打开软件先从music表获取所有数据,添加进一个集合,将集合的内容添加进item,在listview中显示出来 // 如果music表为空,也就是musicList集合长度为0,提示搜索歌曲 runOnUiThread(new Runnable() { @Override public void run() { musicList = myMusicDao.findAll(); if (musicList == null || musicList.size() == 0) { findAllMusic(); } } }); // musicList = new ArrayList<Music>() lv_main_listview.setAdapter(new MyAdapter()); } protected void findAllMusic() { File sdcard = null; if (Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { sdcard = Environment.getExternalStorageDirectory(); Log.i(TAG, sdcard.getPath()); musicList = new ArrayList<Music>(); selectSdcard(sdcard); } else { msg.what = SDCARDERROR; msg.obj = "内存卡不可用"; handler.sendMessage(msg); } } private void selectSdcard(File sdcard) {//我在部署项目的时候回提示我这个方法有空指针异常 Log.i(TAG, sdcard.getPath()); if (sdcard != null && sdcard.exists()) { File[] files = sdcard.listFiles(); if (files != null) { for (File file : files) { Log.i(TAG, file.getPath()); if (file != null && file.isDirectory()) { selectSdcard(file); } else { String name = file.getName(); String end = name.substring(name.lastIndexOf(".") + 1, name.length()); if ("mp3".equalsIgnoreCase(end)) { music = new Music(); music.setName(file.getName()); music.setPath(file.toString()); musicList.add(music); } } } } } } private class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return musicList.size(); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.item_listview, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } holder = (ViewHolder) convertView.getTag(); holder.tv_name.setText(musicList.get(position).getName()); // 设置点击歌曲的方法 holder.tv_name.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String path = musicList.get(position).getPath(); if (mediaPlayer != null || mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(path); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); // 设置点击删除的方法 holder.tv_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String path = musicList.get(position).getPath(); File file = new File(path); if (file.exists()) { Log.i(TAG, "文件存在"); if (file.delete()) { Log.i(TAG, "删除成功"); msg.obj = "删除成功"; msg.what = SUCCESSDELETE; } else { Log.i(TAG, "删除失败"); msg.obj = "删除失败"; msg.what = ERRORDELETE; } } else { Log.i(TAG, "文件不存在,删除当前记录"); msg.obj = "文件不存在,删除当前记录"; msg.what = ITEMDELETE; } handler.sendMessage(msg); } }); return null; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } private class ViewHolder { @ViewInject(R.id.tv_name) private TextView tv_name; @ViewInject(R.id.tv_delete) private TextView tv_delete; public ViewHolder(View view) { ViewUtils.inject(this, view); } } }
运行后提示空指针异常 就在遍历内存卡那个方法那里
按道理讲如果只分析selectSdcard方法只有Log.i(TAG, sdcard.getPath());和musicList.add(music);才会报空指针。但是在调该方法时会写日志且初始化musicList,不应该报空指针呀?另外该方法有多处冗余和逻辑问题。