我使用onQueryTextChange()来使用BaseAdapter for ListView从列表中搜索文本。现在我在搜索时得到Null Pointer Exception。所以请整理出来。在某处搜索时,不会从列表中获取值。
public class GetListView extends ListActivity implements OnQueryTextListener {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
HashMap<String, String> map = new HashMap<String, String>();
private static String[] data;
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_IMG = "img";
private static final String TAG_PRICE = "price";
private static final String TAG_DESTINATION = "destination";
JSONArray products = null;
ListView lv;
LazyImageLoadAdapter adapter;
private String[] str;
private String[] mPid;
private String[] mName;
private String[] mPrice;
private String[] mDestination;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent iN = getIntent();
data = iN.getStringArrayExtra("getdata");
setTitle(data[1]);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
lv = getListView();
lv.setBackgroundResource(R.drawable.background);
lv.setTextFilterEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
// searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
if (TextUtils.isEmpty(newText)) {
lv.clearTextFilter();
} else {
lv.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(GetListView.this);
pDialog.setMessage("Loading data.. Please wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(data[0], "GET", params);
Log.d("Gellting all the list : ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
str = new String[products.length()];
mPid = new String[products.length()];
mName = new String[products.length()];
mPrice = new String[products.length()];
mDestination = new String[products.length()];
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
String destination = c.getString(TAG_DESTINATION);
String img = c.getString(TAG_IMG);
str[i] = img;
mPid[i] = id;
mName[i] = name;
mPrice[i] = price;
mDestination[i] = destination;
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
adapter = new LazyImageLoadAdapter(GetListView.this, context, str, mPid, mName, mPrice, mDestination);
getListView().setAdapter(adapter);
getListView().setBackgroundResource(R.drawable.background);
}
});
}
}
}
错误:
06-11 04:19:38.759: D/AndroidRuntime(1334): Shutting down VM
06-11 04:19:38.759: W/dalvikvm(1334): threadid=1: thread exiting with uncaught exception (group=0xb4a6dba8)
06-11 04:19:38.819: E/AndroidRuntime(1334): FATAL EXCEPTION: main
06-11 04:19:38.819: E/AndroidRuntime(1334): Process: com.ambrogroup, PID: 1334
06-11 04:19:38.819: E/AndroidRuntime(1334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ambrogroup/com.ambrogroup.GetListView}: java.lang.NullPointerException
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.os.Looper.loop(Looper.java:136)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-11 04:19:38.819: E/AndroidRuntime(1334): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 04:19:38.819: E/AndroidRuntime(1334): at java.lang.reflect.Method.invoke(Method.java:515)
06-11 04:19:38.819: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-11 04:19:38.819: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-11 04:19:38.819: E/AndroidRuntime(1334): at dalvik.system.NativeStart.main(Native Method)
06-11 04:19:38.819: E/AndroidRuntime(1334): Caused by: java.lang.NullPointerException
06-11 04:19:38.819: E/AndroidRuntime(1334): at com.ambrogroup.GetListView.onCreate(GetListView.java:65)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.Activity.performCreate(Activity.java:5231)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-11 04:19:38.819: E/AndroidRuntime(1334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-11 04:19:38.819: E/AndroidRuntime(1334): ... 11 more