in 1 of classes, attempt access view (in main layout) in response broadcast received:
protected broadcastreceiver myreceiver = new broadcastreceiver() { @override public void onreceive(context ctx, intent intent) { string action = intent.getaction(); if ( action.equals("com.mydomain.myapp.interesting_event_occurred") ) { ((activity) ctx).setcontentview(r.layout.main); linearlayout linlayout = (linearlayout) findviewbyid(r.id.lin_layout); if (linlayout != null) { log.i(tag_ok, "ok proceed accessing views inside layout"); } else log.e(tag_fail, "what's wrong calling findviewbyid inside onreceive()?"); } } };
the problem findviewbyid() returns null , result tag_fail error message.
the same exact findviewbyid(r.id.lin_layout)
call inside activity's oncreate() returns desired result, know it's not typo or other error in code quoted above.
why happening?
is there restriction on on calling findviewbyid() inside broadcastreceiver?
or other reason?
broadcastreceiver own class , not inherit android.app.activity, yes? logic, can't expect include activity's methods.
pass context broadcastreceiver, or more directly, pass reference view want manipulate.
// package protected access linearlayout linlayout; oncreate() { super.oncreate(savedinstancestate); setcontentview(r.layout.main); linlayout = (linearlayout) findviewbyid(r.id.lin_layout); } protected broadcastreceiver myreceiver = new broadcastreceiver() { @override public void onreceive(context ctx, intent intent) { string action = intent.getaction(); if ( action.equals("com.mydomain.myapp.interesting_event_occurred")) { if (linlayout != null) { log.i(tag_ok, "ok proceed accessing views inside layout"); } else log.e(tag_fail, "what's wrong calling findviewbyid inside onreceive()?"); } } };
Comments
Post a Comment