Reputation: 59
I am working on android app and put filtration functionality on Listview
. But I got below error:
java.lang.RuntimeException: Unable to start activity componentInfo:java.lang.NullPointerException: Attempt to invoke virtual method void android.widget.EditText.addTextchangedListener(android.text.TextWatcher) on a Null object Reference
Below is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shopdetail);
aq=new AQuery(this);
value=5;
GPSTracker gps = new GPSTracker(Shopdetail.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
getdatalatlog(latitude,longitude);
}else{
gps.showSettingsAlert();
}
Intent openStartingPoint=getIntent();
String city=openStartingPoint.getStringExtra("spinnerText");
bindcity(city);
final EditText 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
//Shopdetail.this.adapter.getFilter().filter(cs);
//((SimpleAdapter)Shopdetail.this.adapter).getFilter().filter(cs);
// ((SimpleAdapter)getListAdapter()).getFilter().filter(cs);
//get the text in the EditText
EditText inputSearch = (EditText)findViewById(R.id.inputSearch);
String searchString=inputSearch.getText().toString();
int textLength=searchString.length();
searchResults.clear();
for(int i=0;i<mCommentList.size();i++)
{
String shopName=mCommentList.get(i).get(TAG_TITLE).toString();
if(textLength<=shopName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.trim().equalsIgnoreCase(shopName.trim().substring(0,textLength))){
searchResults.add(mCommentList.get(i));
getdata();}
}
}
adapter.notifyDataSetChanged();
}
@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
}
});
}
private void getdata() {
// TODO Auto-generated method stub
String[] data = new String[]{TAG_TITLE,TAG_USERNAME ,TAG_CONTACT ,TAG_MESSAGE ,TAG_LATITUDE , TAG_LONGITUDE };
int[] to= new int[] {R.id.shop_name,R.id.address,R.id.contact,R.id.distance,R.id.latitude,R.id.longitude};
final SimpleAdapter adapter = new SimpleAdapter(this, mCommentList,R.layout.single_post, data,to);
ListView lv = ( ListView ) findViewById(android.R.id.list);
lv.setAdapter(adapter);
}
Below is my code to firstly bind data.
private void getdatalatlog(double latitude, double longitude) {
// TODO Auto-generated method stub
//value=5;
String url = "http://192.168.2.4/PHP/webservice/comments.php?latitude='"+latitude+"'&longitude='"+longitude+"'&value='"+value+"'";
aq.progress(R.id.progressBar1).ajax(url, JSONObject.class, this,"jsonCallback");
}
@SuppressWarnings("unchecked")
public void jsonCallback(String url, JSONObject json, AjaxStatus status) {
if (json != null) {
List<String> city = new ArrayList<String>();
mCommentList = new ArrayList<HashMap<String, Object>>();
Gson gson = new GsonBuilder().create();
try {
JSONArray jsonResponse = json.getJSONArray("posts");
city = gson.fromJson(jsonResponse.toString(),List.class);
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject c = jsonResponse.getJSONObject(i);
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String username = c.getString(TAG_USERNAME);
String lat = c.getString(TAG_LATITUDE);
String log = c.getString(TAG_LONGITUDE);
String cont = c.getString(TAG_CONTACT);
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_USERNAME, username);
map.put(TAG_LATITUDE, lat);
map.put(TAG_LONGITUDE, log);
map.put(TAG_CONTACT, cont);
// adding HashList to ArrayList
mCommentList.add(map);
searchResults=new ArrayList<HashMap<String,Object>>(mCommentList);
//HashMap<String, String> map1 = new HashMap<String, String>();
//map1.put(TAG_TITLE, title);
//sortdata;
}
}
catch (JSONException e) {
Toast.makeText(aq.getContext(), "Error in parsing JSON", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(aq.getContext(), "Something went wrong", Toast.LENGTH_LONG).show();
}
String[] data = new String[]{TAG_TITLE,TAG_USERNAME ,TAG_CONTACT ,TAG_MESSAGE ,TAG_LATITUDE , TAG_LONGITUDE };
int[] to= new int[] {R.id.shop_name,R.id.address,R.id.contact,R.id.distance,R.id.latitude,R.id.longitude};
final SimpleAdapter adapter = new SimpleAdapter(this, mCommentList,R.layout.single_post, data,to);
ListView lv = ( ListView ) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();
Bundle b = new Bundle();
String name= ((TextView) view.findViewById(R.id.shop_name)).getText().toString()+" , "+((TextView) view.findViewById(R.id.contact)).getText().toString();
String lati= ((TextView) view.findViewById(R.id.latitude)).getText().toString();
String longi= ((TextView) view.findViewById(R.id.longitude)).getText().toString();
b.putString("shop",name);
b.putString("lati",lati);
b.putString("long",longi);
intent.setClass(Shopdetail.this, Mapview.class);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
When user type letter on EditText the data is filtered and its bind in listview according to that. Its not working. Please guide me.
Thanks so much in advance.
Upvotes: 0
Views: 12701
Reputation: 2155
Invoking method on Null object reference will always throw java.lang.NullPointerException.
In your case, logs clearly indicate "Attempt to invoke method addTextchangedListener(android.text.TextWatcher) on a Null object Reference".
In short - inputSearch is null and should be initialized before any method invocations.
Upvotes: 1
Reputation: 47817
You forget to initialized inputSearch
inputSearch=(EditText)findViewById(R.id.editTextId);
OR move
EditText inputSearch = (EditText)findViewById(R.id.inputSearch);
before
inputSearch.addTextChangedListener(new TextWatcher() {
Upvotes: 10