java - Fragment - Cannot cast from Context in 'CustomAutoCompleteTextChangedListener' -


i'm using auto complete text listener in sqlite @ fragment java class, unfortunately i'm having error nullpointerexception. here logcat:

05-12 09:15:05.987: e/androidruntime(20901): fatal exception: main 05-12 09:15:05.987: e/androidruntime(20901): process: iot.bayadcenter.slidingmenu, pid: 20901 05-12 09:15:05.987: e/androidruntime(20901): java.lang.nullpointerexception 05-12 09:15:05.987: e/androidruntime(20901):    @ iot.bayadcenter.slidingmenu.billers.payment.getitemsfromdb(payment.java:271) 05-12 09:15:05.987: e/androidruntime(20901):    @ iot.bayadcenter.searchbarsqlite.customautocompletetextchangedlistener.ontextchanged(customautocompletetextchangedlistener.java:42) 05-12 09:15:05.987: e/androidruntime(20901):    @ android.widget.textview.sendontextchanged(textview.java:7610) 05-12 09:15:05.987: e/androidruntime(20901):    @ android.widget.textview.handletextchanged(textview.java:7672) 

and here customautocompletetextchangedlistener.java

public class customautocompletetextchangedlistener implements textwatcher{  public static final string tag = "customautocompletetextchangedlistener.java"; context context;  public customautocompletetextchangedlistener(context context){     this.context = context; }   @override public void ontextchanged(charsequence userinput, int start, int before, int count) {      // if want see in logcat user types     log.e(tag, "user input: " + userinput);      payment mainactivity = new payment();      // query database based on user input     mainactivity.item = mainactivity.getitemsfromdb(userinput.tostring());//here line 42      // update adapater     mainactivity.myadapter.notifydatasetchanged();     mainactivity.myadapter = new arrayadapter<string>(mainactivity.getactivity(), android.r.layout.simple_dropdown_item_1line, mainactivity.item);     mainactivity.myautocomplete.setadapter(mainactivity.myadapter);  } 

}

and here fragment(payment.java)

public class payment extends fragment {  customautocompleteview myautocomplete;      // adapter auto-complete     arrayadapter<string> myadapter;      // database operations     databasehandler databaseh;      // add initial value     string[] item = new string[] {"please search..."};      context ctx = getactivity();      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {              myautocomplete = (customautocompleteview) rootview.findviewbyid(r.id.myautocomplete);              // add listener tries suggest while user types             myautocomplete.addtextchangedlistener(new customautocompletetextchangedlistener(getactivity()));              // set our adapter             myadapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_dropdown_item_1line, item);             myautocomplete.setadapter(myadapter); 

and here other part:

// function used in customautocompletetextchangedlistener.java     public string[] getitemsfromdb(string searchterm){          // add items on array dynamically         list<myobject> products = databaseh.read(searchterm);//here line 27         int rowcount = products.size();          string[] item = new string[rowcount];         int x = 0;          (myobject record : products) {              item[x] = record.objectname;             x++;         }          return item;     } 

since here full bunch of exceptions, let's start first 1 visible. line #271 produces nullpointerexception, is

list<myobject> products = databaseh.read(searchterm); 

i guess because databaseh null. check if open connection properly. line made me wondered payment mainactivity = new payment();. create new instance of fragment in custom class implements textwatcher... don't call of methods fragment unless not on screen. fragments used attached activity, not handling database methods. take @ pattern mvc. think here not sense fix bugs, better reorganize everything. hope helps @ least little.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -