Good day! I decided to write an application player that scans the device, adds tracks to the playlist, and at the touch of a button opens it. But there was a problem: when you click on the "Playlist" button, an error java.lang.NullPointerException appears, indicating a ListView that is defined. Here is the code:
public ArrayAdapter mAdapter; public ListView mListView; private static final String[] EXTENSIONS = { ".mp3", ".wav", ".ogg", ".mus", ".aac" }; public List<String> trackNames; public List<String> trackArtworks;
public String[] getTracks() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); String[] temp = path.list(); return temp; } else { Toast.makeText(getBaseContext(), "SD-карта не может быть прочитана или повреждена", Toast.LENGTH_LONG).show(); } return null; } public void addTracks(String[] temp) { if (temp != null) { for (int i = 0; i < temp.length; i++) { if (trackChecker(temp[i])) { trackNames.add(temp[i]); trackArtworks.add(temp[i].substring(0, temp[i].length() - 4)); ListView mListView = (ListView) findViewById(R.id.listsong_screen); ArrayAdapter mAdapter = new ArrayAdapter(this, R.layout.listsong_item, trackNames); } } Toast.makeText(getBaseContext(), "Загружено " + Integer.toString(trackNames.size()) + " треков", Toast.LENGTH_SHORT).show(); } } private boolean trackChecker(String trackToTest) { for (int j = 0; j < EXTENSIONS.length; j++) { if (trackToTest.contains(EXTENSIONS[j])) { return true; } } return false; } private void loadTrack() { if (mediaPlayer != null) { dispose(); } if (trackNames.size() > 0) { loadMusic(); } }
Button playlist = (Button) findViewById(R.id.playlistbutton); playlist.setOnClickListener(new OnClickListener(this) { public void onClick(View v) { mListView.setAdapter(mAdapter) } });