我想使用编辑文本在我的列表中搜索,但是当我尝试在编辑文本中输入内容时,应用会停止并在控制台中显示以下错误。
02-08 09:46:23.675: E/AndroidRuntime(3759): FATAL EXCEPTION: main
02-08 09:46:23.675: E/AndroidRuntime(3759): java.lang.NullPointerException
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.example.search.MainActivity$JSONfunctions.onTextChanged(MainActivity.java:142)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.widget.TextView.handleTextChanged(TextView.java:7151)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:222)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.widget.TextView.doKeyDown(TextView.java:5246)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.widget.TextView.onKeyDown(TextView.java:5065)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.View.dispatchKeyEvent(View.java:7198)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.os.Looper.loop(Looper.java:137)
02-08 09:46:23.675: E/AndroidRuntime(3759): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-08 09:46:23.675: E/AndroidRuntime(3759): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 09:46:23.675: E/AndroidRuntime(3759): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-08 09:46:23.675: E/AndroidRuntime(3759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-08 09:46:23.675: E/AndroidRuntime(3759): at dalvik.system.NativeStart.main(Native Method)
我的代码:
public class MainActivity extends ListActivity {
SimpleAdapter adapter;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
JSONfunctions jsfn=new JSONfunctions();
jsfn.execute("http://192.168.6.43/employees.php");
}
public class JSONfunctions extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > implements TextWatcher {
InputStream is;
String result ;
JSONObject jArray;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params)
{
try{
/*get jobject from the url
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
// MainActivity.dataFromAsyncTask=jArray;
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}*/
try{
JSONArray ip = jArray.getJSONArray("Employees");
for(int i=0;i<ip.length();i++){
JSONArray e = ip.getJSONArray(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", e.getString(0)+" "+e.getString(1)+" "+e.getString(2));
//map.put("email id",e.getString(3));
//map.put("phone no",e.getString(4));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return mylist;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result)
{
ListAdapter adapter = new SimpleAdapter(MainActivity.this, result , R.layout.activity_main,
new String[] { "name" },
new int[] { R.id.item_title });
MainActivity.this.setListAdapter(adapter);// If Activity extends ListActivity
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
EditText myFilter = (EditText) findViewById(R.id.editText1);
myFilter.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilter().filter(s.toString());
}
}
}
答案 0 :(得分:0)
您永远不会初始化在onTextChanged
方法中使用的适配器变量。所以你要写:
adapter = new SimpleAdapter(MainActivity.this, result , R.layout.activity_main, new String[] { "name" }, new int[] { R.id.item_title });
<{1>}方法中的或您使用onPostExecute
方法中的以下代码过滤ListView
:
onTextXhanged