gpt4 book ai didi

java - 过滤 SimpleAdapter 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 09:29:45 25 4
gpt4 key购买 nike

问题:我只想过滤或搜索 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"));
}
}
}

最佳答案

没有看到异常的堆栈跟踪,我假设 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);

关于java - 过滤 SimpleAdapter 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13540033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com