我的应用正在从JSON网址解析数据并将其显示在列表视图中。
我想创建一个搜索栏来搜索lisview中的项目,但是当我尝试搜索某些内容时出现NullPointerException错误。
我认为必须对适配器执行某些操作。
这是我的代码:
public class MainActivity extends ListActivity {
// Search EditText
EditText inputSearch;
// Listview Adapter
ArrayAdapter<String> adapter;
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "JSONURL";
// JSON Node names
private static final String TAG_DATA = "data";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_ABV = "abv";
private static final String TAG_IBU = "ibu";
//private static final String TAG_ISORGANIC = "isOrganic";
//private static final String TAG_YEAR = "year";
// contacts JSONArray
JSONArray data = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> dataList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String abv = ((TextView) view.findViewById(R.id.abv))
.getText().toString();
String ibu = ((TextView) view.findViewById(R.id.ibu))
.getText().toString();
//String description = ((TextView) view.findViewById(R.id.description))
//.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_ABV, abv);
in.putExtra(TAG_ABV, ibu);
//in.putExtra(TAG_DESCRIPTION, description);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
data = jsonObj.getJSONArray(TAG_DATA);
// looping through All beers
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String ibu;
if(c.has("ibu")) {
ibu = c.getString(TAG_IBU);
} else {
ibu = "No ibu value";
}
String abv;
if(c.has("abv")) {
abv = c.getString(TAG_ABV);
} else {
abv = "No abv value";
}
String description;
if(c.has("description")) {
description = c.getString(TAG_DESCRIPTION);
} else {
description = "No description available";
}
//String abv = c.getString(TAG_ABV);
//String ibu = c.getString(TAG_IBU);
//String isOrganic = c.getString(TAG_ISORGANIC);
//String year = c.getString(TAG_YEAR);
// tmp hashmap for single contact
HashMap<String, String> data = new HashMap<String, String>();
// adding each child node to HashMap key => value
data.put(TAG_ID, id);
data.put(TAG_NAME, name);
//data.put(TAG_YEAR, year);
data.put(TAG_IBU, ibu);
data.put(TAG_ABV, abv);
data.put(TAG_DESCRIPTION, description);
// adding beer to beer list
dataList.add(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, dataList,
R.layout.list_item, new String[] { TAG_NAME, TAG_IBU, TAG_ABV},
new int[] { R.id.name, R.id.ibu, R.id.abv });
inputSearch = (EditText) findViewById(R.id.inputSearch);
setListAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.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
}
});
}
}
这是错误:
08-31 20:01:40.372 13714-13714/com.javapapers.android.jsonparsing W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41762db8)
08-31 20:01:40.382 13714-13714/com.javapapers.android.jsonparsing E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.javapapers.android.jsonparsing, PID: 13714
java.lang.NullPointerException
at com.peeters.ward.beerdroid.MainActivity$GetContacts$1.onTextChanged(MainActivity.java:249)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.handleTextChanged(TextView.java:7467)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9187)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
08-31 20:01:41.762 13714-13714/com.javapapers.android.jsonparsing I/Process﹕ Sending signal. PID: 13714 SIG: 9
答案 0 :(得分:1)
您在input1 -> check username ->output check result
input2-> check password ->output check result
input3-> check email->output check result
中创建一个名为adapter
的局部变量,然后在文本更改侦听器中调用全局变量。
请致电:
onPostExecute()
不实例化新对象。