java - Android: Passing TextView as parameter to external function returns nullpointerexception -
i'm trying pass textview parameter external class perform functionality i'm hitting nullpointerexception each time.
activity.java
tv = (textview) findviewbyid(r.id.tv); log.v("", "contents: " + tv.gettext().tostring()); // works fine externalclass.addviewtolist(tv); externalclass.printviewcontents(tv.gettext().tostring());
external class
public list<view> _views; // doesn't work public void addviewtolist(view v) { try { this._views.add(v); log.v("", "added " + v.getid() + " list"); } catch(nullpointerexception ex) { log.e("", "nullpointerexception when adding view list"); } } // works fine public void printviewcontents(string contents) { log.v("",contents); }
so know view has been found in layout.xml , has been initialized, question how pass view activity external class.
thanks
edit
forgot lists need initalised arraylist
cheers!
your _views list isnt initialized i.e youre trying write data null. solve need write:
public list<view> _views = new list<>();
instead of just:
public list<view> _views;
for less headache recommend work array list instead:
public arraylist<view> list = new arraylist<>();
now work :)
Comments
Post a Comment