我知道有关于这个主题的帖子,但我找不到我的代码中的问题。尝试 2 天后,我在这里发帖。这是我的适配器:
public class CustomerListBaseAdapter extends BaseAdapter implements Filterable {
private static ArrayList<CustAccountObject> CustArrayList;
private LayoutInflater l_Inflater;
public CustomerListBaseAdapter(Context context, ArrayList<CustAccountObject> results) {
CustArrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return CustArrayList.size();
}
public Object getItem(int position) {
return CustArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.customer_list_format, null);
holder = new ViewHolder();
holder.txt_custName = (TextView) convertView.findViewById(R.id.customer_name);
holder.txt_custEmail = (TextView) convertView.findViewById(R.id.customer_email);
holder.txt_custId = (TextView) convertView.findViewById(R.id.customer_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_custName.setText(CustArrayList.get(position).name);
holder.txt_custEmail.setText(CustArrayList.get(position).email1);
holder.txt_custId.setText(CustArrayList.get(position).pkid.toString());
return convertView;
}
static class ViewHolder {
TextView txt_custName;
TextView txt_custEmail;
TextView txt_custId;
}
@Override
public Filter getFilter()
{
Filter filter = new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults results = new FilterResults();
//If there's nothing to filter on, return the original data for your list
if(charSequence == null || charSequence.length() == 0)
{
results.values = CustArrayList;
results.count = CustArrayList.size();
}
else
{
ArrayList<CustAccountObject> filterResultsData = new ArrayList<CustAccountObject>();
String filterString = charSequence.toString().toLowerCase();
String filterableString;
for(int i=0; i<CustArrayList.size(); i++)
{
filterableString = CustArrayList.get(i).toString();
if(filterableString.toLowerCase().contains(filterString)){
filterResultsData.add(CustArrayList.get(i));
}
}
results.values = filterResultsData;
results.count = filterResultsData.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
CustArrayList = (ArrayList<CustAccountObject>)filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
}
这是我的活动:
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
CustomerListActivity.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
}
});
我已经尝试过调试,但是在使用此inputSearch.addTextChangedListener(new TextWatcher()
代码后,应用程序崩溃了。
我的日志猫:
03-22 04:20:00.644: E/Trace(20950): error opening trace file: No such file or directory (2)
03-22 04:20:01.335: E/AndroidRuntime(20950): FATAL EXCEPTION: main
03-22 04:20:01.335: E/AndroidRuntime(20950): java.lang.RuntimeException: Unable to instantiate application asia.wavelet.android.application.MyApp: java.lang.NullPointerException
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.os.Looper.loop(Looper.java:137)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-22 04:20:01.335: E/AndroidRuntime(20950): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 04:20:01.335: E/AndroidRuntime(20950): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 04:20:01.335: E/AndroidRuntime(20950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 04:20:01.335: E/AndroidRuntime(20950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 04:20:01.335: E/AndroidRuntime(20950): at dalvik.system.NativeStart.main(Native Method)
03-22 04:20:01.335: E/AndroidRuntime(20950): Caused by: java.lang.NullPointerException
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
03-22 04:20:01.335: E/AndroidRuntime(20950): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
03-22 04:20:01.335: E/AndroidRuntime(20950): ... 11 more
03-22 04:21:10.265: E/AndroidRuntime(20984): FATAL EXCEPTION: main
03-22 04:21:10.265: E/AndroidRuntime(20984): java.lang.NullPointerException
03-22 04:21:10.265: E/AndroidRuntime(20984): at asia.wavelet.android.customer.CustomerListActivity$RetreiveCustomer.onPostExecute(CustomerListActivity.java:237)
03-22 04:21:10.265: E/AndroidRuntime(20984): at asia.wavelet.android.customer.CustomerListActivity$RetreiveCustomer.onPostExecute(CustomerListActivity.java:1)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.os.Looper.loop(Looper.java:137)
03-22 04:21:10.265: E/AndroidRuntime(20984): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-22 04:21:10.265: E/AndroidRuntime(20984): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 04:21:10.265: E/AndroidRuntime(20984): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 04:21:10.265: E/AndroidRuntime(20984): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 04:21:10.265: E/AndroidRuntime(20984): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 04:21:10.265: E/AndroidRuntime(20984): at dalvik.system.NativeStart.main(Native Method)
我的 AsyncTask 类:
class RetreiveCustomer extends AsyncTask<Void,Void,String> {
protected void onPreExecute() {
//display progress dialog.
}
protected String doInBackground(Void... params) {
String json = null;
try {
boolean fromLocal = true;
if(isOnline(getApplicationContext())) {
try {
URL url = new URL("http://192.168.0.177:8080/ws/api/customer/5");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int response = con.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
json = readStream(con.getInputStream());
fromLocal = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
if(fromLocal) {
Collection custCol = new Vector();
custCol = custBean.query(null, null, null, CustAccountBean.NAME);
Gson gson = new Gson();
json = gson.toJson(custCol);
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
private String readStream(InputStream in) {
BufferedReader reader = null;
String line = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return line;
}
protected void onPostExecute(String result) {
Collection custCol = new Vector();
Gson gson = new Gson();
Type token = new TypeToken<Collection<CustAccountObject>>() {}.getType();
custCol = gson.fromJson(result, token);
// List<CustAccountObject> customers = new ArrayList<CustAccountObject>(custCol);
//
// lv = (ListView) findViewById(R.id.list);
// inputSearch = (EditText) findViewById(R.id.customer_search);
//
// // Adding customers to listview
// adapter = new ArrayAdapter<CustAccountObject>(getApplicationContext(), R.layout.list, R.id.name, customers);
// lv.setAdapter(adapter);
ArrayList<CustAccountObject> cust_details = GetSearchResults(custCol);
lv = (ListView) findViewById(R.id.list);
adapter = new CustomerListBaseAdapter(getApplicationContext(), cust_details);
lv.setAdapter(adapter);
// inputSearch.addTextChangedListener(new TextWatcher() {
//
// @Override
// public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// // When user changed the Text
// CustomerListActivity.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
// }
// });
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustAccountObject custObj = (CustAccountObject) adapter.getItem(position);
customerEdit(custObj.pkid);
}
});
}
public boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
private ArrayList<CustAccountObject> GetSearchResults(Collection custCol){
ArrayList<CustAccountObject> results = new ArrayList<CustAccountObject>();
Vector vecResult = new Vector(custCol);
CustAccountObject custObj = new CustAccountObject();
for(int i=0; i<vecResult.size(); i++)
{
custObj = (CustAccountObject) vecResult.get(i);
results.add(custObj);
}
return results;
}
}