問題:私が欲しいのは、フィルタリングするか、アイテムを検索することListView
だけですが、常にこのエラーが発生します"java.lang.NullPointerException"
。この問題を解決するのを手伝ってください。
PlayListActivity.java:
package com.name.musicplayer;
import java.util.ArrayList;
import java.util.HashMap;
import com.androidhive.musicplayer.PlayListActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class PlayListActivity extends ListActivity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
//list view adapter
ArrayAdapter<String> adapter_l;
// Search EditText
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
inputSearch = (EditText) findViewById(R.id.inputSearch);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
R.id.songTitle });
setListAdapter(adapter);
met_play_song_on_list();
met_search();
}
private void met_play_song_on_list() {
// TODO Auto-generated method stub
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AndroidBuildingMusicPlayerActivity.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
private void met_search() {
// TODO Auto-generated method stub
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
//PlayListActivity.this.adapter_l.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
PlayListActivity.this.adapter_l.getFilter().filter(arg0);
}
});
}
}
SongManager.javaクラスは次のとおりです。
package com.name.musicplayer;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.name.musicplayer.JSONParser;
public class SongsManager {
// SDCard Path
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// url to make request
private static String url = "http://domain.com/android/songlist.php";
// JSON Node names
private static final String TAG_str_song = "str_song";
private static final String TAG_str_song_name = "str_song_name";
private static final String TAG_str_song_path = "str_song_path";
// contacts JSONArray
JSONArray songslist = null;
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files from sdcard and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
songslist = json.getJSONArray(TAG_str_song);
// looping through All Contacts
for (int i = 0; i < songslist.length(); i++) {
JSONObject c = songslist.getJSONObject(i);
// Storing each json item in variable
String str_song_name = c.getString(TAG_str_song_name);
String str_song_path = c.getString(TAG_str_song_path);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("songTitle", (i+1) +". " + str_song_name);
map.put("songPath", "http://domain.com/" + str_song_path);
// adding HashList to ArrayList
songsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return songsList;
}
/**
* Class to filter files which are having .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}