I have an activity with a BroadcastReceiver in it as shown in this code:
public class MyActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.activity_myActivity);
int method = Intent.GetIntExtra(KEY_MYACTIVITY_METHOD, METHOD_MYACTIVITY);
mAlgo= new algo(this);
intent = new Intent(this, typeof( BroadcastService) ); //*****
}
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
private class broadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
updateUI(intent);
}
private void updateUI(Intent intent)
{
float mx = mAlgo.getmX();
TextView startx =FindViewById<TextView>(Resource.Id.startx); //ERROR
}
}
}
I have the error with FindViewById which tell that An object reference is required for the property , method, or the non-static field Activity.FindViewById(int)'. Can you see what is going wrong? thank you
You can't call FindViewById
from a nested class. You could:
1) Hold a reference to the activity object inside the nested broadcastReceiver
class:
public class MyActivity : Activity {
...
private class broadCastReceiver : BroadcastReceiver {
private MyActivity act;
public broadCastReceiver (MyActivity act) {
this.act = act;
}
private void updateUI (Intent intent) {
TextView startx = act.FindViewById<TextView> (Resource.Id.startx);
}
}
}
2) Or hold a reference to the TextView
in the activity and pass it to the broadcast receiver similarly like in the 1st example.