Android 使用EditText进行搜索
你能帮我做这个吗。我想使用editText为我的listview创建搜索。一切正常,但当我运行应用程序时,它会导致运行时错误,并且我在AlbumActivity.onTextChanged上得到一个NullPointerException。你知道怎么解决这个问题吗?谢谢代码如下:Android 使用EditText进行搜索,android,nullpointerexception,android-edittext,Android,Nullpointerexception,Android Edittext,你能帮我做这个吗。我想使用editText为我的listview创建搜索。一切正常,但当我运行应用程序时,它会导致运行时错误,并且我在AlbumActivity.onTextChanged上得到一个NullPointerException。你知道怎么解决这个问题吗?谢谢代码如下: @SuppressLint("NewApi") public class AlbumsActivity extends ListActivity{ ConnectionDetector cd; AlertDialogM
@SuppressLint("NewApi")
public class AlbumsActivity extends ListActivity{
ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> albumsList;
JSONArray albums = null;
EditText inputSearch;
ArrayAdapter<String> adapter = null;
private static final String URL_ALBUMS = "http://api.androidhive.info/songs/album.php";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_SONGS_COUNT = "songs_count";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
cd = new ConnectionDetector(getApplicationContext());
if (!cd.isConnectingToInternet()) {
alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
return;
}
albumsList = new ArrayList<HashMap<String, String>>();
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
AlbumsActivity.this.adapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
new LoadAlbums().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), TrackListActivity.class);
String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
i.putExtra("album_id", album_id);
startActivity(i);
}
});
}
class LoadAlbums extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlbumsActivity.this);
pDialog.setMessage("Listing Albums ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
params);
Log.d("Albums JSON: ", "> " + json);
try {
albums = new JSONArray(json);
if (albums != null) {
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String songs_count = c.getString(TAG_SONGS_COUNT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_SONGS_COUNT, songs_count);
albumsList.add(map);
}
}else{
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
AlbumsActivity.this, albumsList,
R.layout.list_item_albums, new String[] { TAG_ID,
TAG_NAME, TAG_SONGS_COUNT }, new int[] {
R.id.album_id, R.id.album_name, R.id.songs_count });
setListAdapter(adapter);
}
});
}
}
@SuppressLint(“NewApi”)
公共类AlbumsActivity扩展了ListActivity{
连接检测器cd;
AlertDialogManager alert=新建AlertDialogManager();
私人对话;
JSONParser JSONParser=新的JSONParser();
ArrayList专辑列表;
JSONArray相册=null;
编辑文本输入搜索;
ArrayAdapter适配器=空;
私有静态最终字符串URL_相册=”http://api.androidhive.info/songs/album.php";
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u SONGS\u COUNT=“SONGS\u COUNT”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u相册);
getActionBar().setBackgroundDrawable(新的ColorDrawable(Color.parseColor(“#3b5998”));
getActionBar().setIcon(新的ColorDrawable(getResources().getColor(android.R.color.transparent));
cd=新连接检测器(getApplicationContext());
如果(!cd.isConnectingToInternet()){
alert.showAlertDialog(AlbumsActivity.this,“Internet连接错误”,
“请连接到正在工作的Internet连接”,错误);
返回;
}
albumsList=新的ArrayList();
inputSearch=(EditText)findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
AlbumsActivity.this.adapter.getFilter().filter(cs.toString());
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
});
新建LoadAlbums().execute();
ListView lv=getListView();
setOnItemClickListener(新的android.widget.AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView arg0、View视图、int arg2、,
长arg3){
Intent i=新的Intent(getApplicationContext(),TrackListActivity.class);
字符串album_id=((TextView)view.findviewbyd(R.id.album_id)).getText().toString();
i、 putExtra(“唱片集id”,唱片集id);
星触觉(i);
}
});
}
类LoadAlbums扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(AlbumsActivity.this);
setMessage(“列出相册…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
String json=jsonParser.makeHttpRequest(URL_相册,“GET”,
参数);
Log.d(“相册JSON:”,“>”+JSON);
试试{
albums=新JSONArray(json);
如果(相册!=null){
对于(int i=0;i
}您的适配器未初始化。您可以拨打以下电话:
AlbumsActivity.this.adapter.getFilter().filter(cs.toString());
这将不起作用,因为适配器为空。您在onPostExecute
中调用setListAdapter
,但这是一种使用内部适配器的ListActivity
方法,它不会初始化您自己的成员变量
相反,您可以在onPostExecute中执行此操作:
AlbumsActivity.this.adapter = new SimpleAdapter(AlbumsActivity.this, albumsList, R.layout.list_item_albums, new String[] { TAG_ID, TAG_NAME, TAG_SONGS_COUNT }, new int[] {R.id.album_id, R.id.album_name, R.id.songs_count });
// updating listview
setListAdapter(AlbumsActivity.this.adapter);
您确定-
activity\u albums.xml
包含类型为EditText
的inputSearch
?@Ved Prakash是的,它包含id为inputSearch的EditText。在运行时,当我开始键入要搜索的内容时,它会出错。请检查您为onTextChanged
传递的所有值,并确保它们不为空。@Ved Prakash我猜我传递的值为空。我不知道如何解决这个问题看看这篇文章-。请参见如何在onPostExecute()
中初始化适配器以及如何设置列表适配器。您必须这样做。它有一个错误-类型不匹配:无法从SimpleAdapter转换为ArrayAdapter将适配器的类型更改为SimpleAdapter,而不是ArrayAdapter;谢谢它的工作。但如果我输入一个数字或数字。它复制正在搜索的项目