2012-11-24 130 views
2

問題:我想要的只是過濾或讓搜索ListView上的項目,但我總是以這個錯誤"java.lang.NullPointerException"結束。請幫我解決這個問題。過濾SimpleAdapter時出現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")); 
    } 
} 
} 
+0

你在哪一行代碼中得到'NullPointerException'? – Pratik

回答

2

沒有看到有異常的堆棧跟蹤我假設NullPointerException來自使用EditText過濾適配器:

PlayListActivity.this.adapter_l.getFilter().filter(arg0); 

上面的代碼會拋出一個NullPointerException,因爲adapter_lnull,因爲您不會在代碼中的任何位置爲其分配有效的引用。你應該修改onCreate這樣的方法:

adapter_l = new SimpleAdapter(this, songsListData, 
      R.layout.playlist_item, new String[] { "songTitle" }, new int[] { 
        R.id.songTitle }); 
setListAdapter(adapter_l); 
+0

謝謝你,你是最好的,我沒有注意到...現在它的工作... ...你救我的生活,我也改變了setListAdapter(adapter_l);以及ArrayAdapter adapter_l;以SimpleAdapter adapter_l; ...再次感謝...現在我可以休息 – user1849177

+0

@ user1849177沒問題。但是,對於將來的問題,您應該始終發佈您從logcat獲得的任何異常,這樣用戶可以更輕鬆地爲您提供幫助。 – Luksprog

+0

我可以過濾播放列表,但是當我點擊過濾播放列表上的任何項目時,則單擊的歌曲不會播放。爲什麼? 請幫忙... – StarWars

相關問題