android - Custom onClick attribute is not working -
i created compound view consisting of 1 textview
, 1 "buy" button
.
for compound view, made custom attribute called onbuyclick
. supposed serve same purpose view
's onclick
attribute, have listener not on whole view on button. so, first thing did copy view
class onclick
attribute does. , worked! (with minor adaptations).
but add btnbuy.setonclicklistener(new onclicklistener() {
, becomes unable find method want , throws illegalstateexception
(the second one, nosuchmethodexception
being caught).
here piece executed once onbuyclick
attribute:
if (context.isrestricted()) { throw new illegalstateexception("the android:onclick attribute cannot " + "be used within restricted context"); } final string handlername = a.getstring(attr); if (handlername != null) { btnbuy.setonclicklistener(new onclicklistener() { //as "btnbuy." added, crashes once button pressed. private method mhandler; public void onclick(view v) { if (mhandler == null) { try { mhandler = getcontext().getclass().getmethod(handlername, view.class); } catch (nosuchmethodexception e) { int id = getid(); string idtext = id == no_id ? "" : " id '" + getcontext().getresources().getresourceentryname( id) + "'"; throw new illegalstateexception("could not find method " + handlername + "(view) in activity " + getcontext().getclass() + " onclick handler" + " on view " + this.getclass() + idtext, e); } } try { mhandler.invoke(getcontext(), this); } catch (illegalaccessexception e) { throw new illegalstateexception("could not execute non " + "public method of activity", e); } catch (invocationtargetexception e) { throw new illegalstateexception("could not execute " + "method of activity", e); } } }); }
and here variables defined outside piece: btnbuy
, "buy" button
in compound view, , a
typedarray
containing attributes.
can please me?
Comments
Post a Comment