本文介绍了显示java.lang.NullPointerException AllSuggestionsActivity.onTextChanged(AllSuggestionsActivity.this.adapter.getFilter()过滤器(CS)。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到一个错误(显示java.lang.NullPointerException
),当我执行code为的ListView
与的EditText
在该行过滤
AllSuggestionsActivity.this.adapter.getFilter()过滤器(CS);
好心帮。
公共类AllSuggestionsActivity扩展ListActivity { 的EditText inputSearch;
ListView的lstList; //进度对话框
私人ProgressDialog pDialog; ArrayAdapter<串GT;适配器= NULL; //创建JSON解析器对象
JSONParser jParser =新JSONParser(); ArrayList的<&HashMap的LT;字符串,字符串>> suggestionsList; // URL获得所有建议列表
私人静态字符串url_all_suggestions =http://10.0.2.2/JKUAT-M-SUGGESTION-BOX/get_all_suggestions.php; // JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_SUGGESTIONS =建议;
私有静态最后弦乐TAG_SID =SID;
私有静态最后弦乐TAG_SUBJECT =主题; //建议JSONArray
JSONArray建议= NULL; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.all_suggestions); inputSearch =(EditText上)findViewById(R.id.inputSearch);
lstList =(ListView控件)findViewById(android.R.id.list);
//为哈希映射的ListView
suggestionsList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); //在后台线程加载建议
新LoadAllSuggestions()执行(); //获取列表视图
ListView控件LV = getListView(); / **
*启用搜索过滤器
* * /
inputSearch.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
//当用户更改文本
。AllSuggestionsActivity.this.adapter.getFilter()过滤器(CS);
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
//在选课单建议
//启动编辑建议画面
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
//从选定的ListItem得到的值
字符串SID =((的TextView)view.findViewById(R.id.sid))的getText()。
的ToString(); //开始新意图
在意向=新意图(getApplicationContext()
EditSuggestionActivity.class);
//发送到SID下一个活动
in.putExtra(TAG_SID,SID); //开始新的活动,期待一些回响应
startActivityForResult(在100);
}
}); } //从编辑建议活动响应
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
//如果结果code 100
如果(结果code == 100){
//如果结果code 100接收
//意味着用户编辑/删除的建议
//重新载入该画面
意向意图= getIntent();
完();
startActivity(意向);
} } / **
*背景异步任务以通过HTTP请求负载的所有建议
* * /
类LoadAllSuggestions扩展的AsyncTask<字符串,字符串,字符串> { / **
*启动后台线程显示进度对话框之前
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(AllSuggestionsActivity.this);
pDialog.setMessage(加载所有的建议,请稍候......);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} / **
*让所有的网址建议
* * /
@覆盖
保护字符串doInBackground(字符串参数... args){
//大厦参数
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
// URL从获取JSON字符串
JSONObject的JSON = jParser.makeHtt prequest(url_all_suggestions,
GET,则params); //检查JSON效应初探日志的猫
Log.d(所有建议:json.toString()); 尝试{
//检查成功TAG
INT成功= json.getInt(TAG_SUCCESS); 如果(成功== 1){
发现//建议
//获取建议的数组
建议= json.getJSONArray(TAG_SUGGESTIONS); //通过所有循环的建议
的for(int i = 0; I< suggestions.length();我++){
JSONObject的C = suggestions.getJSONObject(I) //存储在变量中的每个JSON项目
字符串ID = c.getString(TAG_SID);
字符串主题= c.getString(TAG_SUBJECT); //创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_SID,身份证);
map.put(TAG_SUBJECT,学科); //添加HashList到ArrayList的
suggestionsList.add(地图);
}
}其他{
//没有发现任何建议
意图I =新意图(getApplicationContext()
MainScreenActivity.class);
//关闭所有previous活动
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
}
}赶上(JSONException E){
e.printStackTrace();
} 返回null;
} / **
*在完成后台任务之后辞退进度对话框
* ** /
@覆盖
保护无效onPostExecute(字符串FILE_URL){
//让所有建议后关闭该对话框
pDialog.dismiss();
//从后台线程更新界面
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
/ **
*更新解析JSON数据到ListView控件
* * /
ListAdapter适配器=新SimpleAdapter(
AllSuggestionsActivity.this,suggestionsList,
R.layout.list_item,新的String [] {TAG_SID,
TAG_SUBJECT},新的INT [] {R.id.sid,
R.id.subject});
//更新列表视图
setListAdapter(适配器);
}
});
}
}
}
XML code为ListView:
<?XML版本=1.0编码=UTF-8&GT?;<滚动型
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:fillViewport =真正的> < LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> <的EditText
机器人:ID =@ + ID / inputSearch
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:EMS =10
机器人:MAXLINES =1
机器人:提示=搜索>
< requestFocus的/>
&所述; /的EditText&GT - →; <! - 主要的ListView
总是给值id作为列表(@android:ID /列表)
- >
< ListView控件
机器人:ID =@机器人:ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/> < / LinearLayout中>
在此输入code< /滚动型>
解决方案
请找到解决错误:
公共类AllSuggestionsActivity扩展ListActivity {SimpleAdapter适配器;的EditText inputSearch;//进度对话框
私人ProgressDialog pDialog;//创建JSON解析器对象
JSONParser jParser =新JSONParser();ArrayList的<&HashMap的LT;字符串,字符串>> suggestionsList;// URL获得所有建议列表
私人静态字符串url_all_suggestions =http://10.0.2.2/JKUAT-M-SUGGESTION-BOX/get_all_suggestions.php;// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_SUGGESTIONS =建议;
私有静态最后弦乐TAG_SID =SID;
私有静态最后弦乐TAG_SUBJECT =主题;//建议JSONArray
JSONArray建议= NULL;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.all_suggestions); //为哈希映射的ListView
suggestionsList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); //在后台线程加载建议
新LoadAllSuggestions()执行(); //获取列表视图
ListView控件LV = getListView(); //在选课单建议
//启动编辑建议画面
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
//从选定的ListItem得到的值
字符串SID =((的TextView)view.findViewById(R.id.sid))的getText()。
的ToString(); //开始新意图
在意向=新意图(getApplicationContext()
EditSuggestionActivity.class);
//发送到SID下一个活动
in.putExtra(TAG_SID,SID); //开始新的活动,期待一些回响应
startActivityForResult(在100);
}
});}//从编辑建议活动响应
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
//如果结果code 100
如果(结果code == 100){
//如果结果code 100接收
//意味着用户编辑/删除的建议
//重新载入该画面
意向意图= getIntent();
完();
startActivity(意向);
}}/ **
*背景异步任务以通过HTTP请求负载的所有建议
* * /
类LoadAllSuggestions扩展的AsyncTask<字符串,字符串,字符串> { / **
*启动后台线程显示进度对话框之前
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(AllSuggestionsActivity.this);
pDialog.setMessage(加载所有的建议,请稍候......);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} / **
*让所有的网址建议
* * /
@覆盖
保护字符串doInBackground(字符串参数... args){
//大厦参数
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
// URL从获取JSON字符串
JSONObject的JSON = jParser.makeHtt prequest(url_all_suggestions,
GET,则params); //检查JSON效应初探日志的猫
Log.d(所有建议:json.toString()); 尝试{
//检查成功TAG
INT成功= json.getInt(TAG_SUCCESS); 如果(成功== 1){
发现//建议
//获取建议的数组
建议= json.getJSONArray(TAG_SUGGESTIONS); //通过所有循环的建议
的for(int i = 0; I< suggestions.length();我++){
JSONObject的C = suggestions.getJSONObject(I) //存储在变量中的每个JSON项目
字符串ID = c.getString(TAG_SID);
字符串主题= c.getString(TAG_SUBJECT); //创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_SID,身份证);
map.put(TAG_SUBJECT,学科); //添加HashList到ArrayList的
suggestionsList.add(地图);
}
}其他{
//没有发现任何建议
意图I =新意图(getApplicationContext()
MainScreenActivity.class);
//关闭所有previous活动
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
}
}赶上(JSONException E){
e.printStackTrace();
} 返回null;
} / **
*在完成后台任务之后辞退进度对话框
* ** /
@覆盖
保护无效onPostExecute(字符串FILE_URL){
//让所有建议后关闭该对话框
pDialog.dismiss();
//从后台线程更新界面
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
/ **
*更新解析JSON数据到ListView控件
* * / 适配器=新SimpleAdapter(
AllSuggestionsActivity.this,suggestionsList,
R.layout.list_item,新的String [] {TAG_SID,
TAG_SUBJECT},新的INT [] {R.id.sid,
R.id.subject});
//更新列表视图
setListAdapter(适配器); / **
*启用搜索过滤器
* * /
inputSearch =(EditText上)findViewById(R.id.inputSearch); inputSearch.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
//当用户更改文本
。AllSuggestionsActivity.this.adapter.getFilter()过滤器(CS);
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
}
});
}
}
}
xml文件保持完好
I am getting an error (java.lang.NullPointerException
) when I execute the code for the ListView
with EditText
for filtering at the line
AllSuggestionsActivity.this.adapter.getFilter().filter(cs);
kindly help.
public class AllSuggestionsActivity extends ListActivity {
EditText inputSearch;
ListView lstList;
// Progress Dialog
private ProgressDialog pDialog;
ArrayAdapter<String> adapter = null;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> suggestionsList;
// url to get all suggestions list
private static String url_all_suggestions = "http://10.0.2.2/JKUAT-M-SUGGESTION-BOX/get_all_suggestions.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_SUGGESTIONS = "suggestions";
private static final String TAG_SID = "sid";
private static final String TAG_SUBJECT = "subject";
// suggestions JSONArray
JSONArray suggestions = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_suggestions);
inputSearch = (EditText) findViewById(R.id.inputSearch);
lstList = (ListView) findViewById(android.R.id.list);
// Hashmap for ListView
suggestionsList = new ArrayList<HashMap<String, String>>();
// Loading suggestions in Background Thread
new LoadAllSuggestions().execute();
// Get listview
ListView lv = getListView();
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
AllSuggestionsActivity.this.adapter.getFilter().filter(cs);
}
@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
}
});
// on seleting single suggestion
// launching Edit Suggestion Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String sid = ((TextView) view.findViewById(R.id.sid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditSuggestionActivity.class);
// sending sid to next activity
in.putExtra(TAG_SID, sid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Suggestion Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted suggestion
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all suggestion by making HTTP Request
* */
class LoadAllSuggestions extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllSuggestionsActivity.this);
pDialog.setMessage("Loading all suggestions. Please wait.......");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All suggestions from url
* */
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_suggestions,
"GET", params);
// Check your log cat for JSON reponse
Log.d("All Suggestions: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// suggestions found
// Getting Array of Suggestions
suggestions = json.getJSONArray(TAG_SUGGESTIONS);
// looping through All Suggestions
for (int i = 0; i < suggestions.length(); i++) {
JSONObject c = suggestions.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_SID);
String subject = c.getString(TAG_SUBJECT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_SID, id);
map.put(TAG_SUBJECT, subject);
// adding HashList to ArrayList
suggestionsList.add(map);
}
} else {
// no suggestions found
Intent i = new Intent(getApplicationContext(),
MainScreenActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all suggestions
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllSuggestionsActivity.this, suggestionsList,
R.layout.list_item, new String[] { TAG_SID,
TAG_SUBJECT }, new int[] { R.id.sid,
R.id.subject });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
xml code for the listview:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/inputSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10"
android:maxLines="1"
android:hint="Search" >
<requestFocus />
</EditText>-->
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
enter code here
</ScrollView>
解决方案
Kindly find solution to the error:
public class AllSuggestionsActivity extends ListActivity {
SimpleAdapter adapter;
EditText inputSearch;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> suggestionsList;
// url to get all suggestions list
private static String url_all_suggestions = "http://10.0.2.2/JKUAT-M-SUGGESTION-BOX/get_all_suggestions.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_SUGGESTIONS = "suggestions";
private static final String TAG_SID = "sid";
private static final String TAG_SUBJECT = "subject";
// suggestions JSONArray
JSONArray suggestions = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_suggestions);
// Hashmap for ListView
suggestionsList = new ArrayList<HashMap<String, String>>();
// Loading suggestions in Background Thread
new LoadAllSuggestions().execute();
// Get listview
ListView lv = getListView();
// on seleting single suggestion
// launching Edit Suggestion Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String sid = ((TextView) view.findViewById(R.id.sid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditSuggestionActivity.class);
// sending sid to next activity
in.putExtra(TAG_SID, sid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Suggestion Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted suggestion
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all suggestion by making HTTP Request
* */
class LoadAllSuggestions extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllSuggestionsActivity.this);
pDialog.setMessage("Loading all suggestions. Please wait.......");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All suggestions from url
* */
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_suggestions,
"GET", params);
// Check your log cat for JSON reponse
Log.d("All Suggestions: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// suggestions found
// Getting Array of Suggestions
suggestions = json.getJSONArray(TAG_SUGGESTIONS);
// looping through All Suggestions
for (int i = 0; i < suggestions.length(); i++) {
JSONObject c = suggestions.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_SID);
String subject = c.getString(TAG_SUBJECT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_SID, id);
map.put(TAG_SUBJECT, subject);
// adding HashList to ArrayList
suggestionsList.add(map);
}
} else {
// no suggestions found
Intent i = new Intent(getApplicationContext(),
MainScreenActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all suggestions
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
AllSuggestionsActivity.this, suggestionsList,
R.layout.list_item, new String[] { TAG_SID,
TAG_SUBJECT }, new int[] { R.id.sid,
R.id.subject });
// updating listview
setListAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
AllSuggestionsActivity.this.adapter.getFilter().filter(cs);
}
@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
}
});
}
});
}
}
}
xml file remains intact
这篇关于显示java.lang.NullPointerException AllSuggestionsActivity.onTextChanged(AllSuggestionsActivity.this.adapter.getFilter()过滤器(CS)。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!