Hello!
Again me. I found a strange problem. Is it possible, that i miss something but i can't solve the problem.
I created a service. This service only get my current GPS position every 1 minute. I found one working method to update UI with this Position from service: Send broadcast message. If i get the position i send to broadcast:
Intent broadcastIntent = new Intent("NewCoordinate");
broadcastIntent.PutExtra("Latitude", Location.Latitude);
broadcastIntent.PutExtra("Longitude", Location.Longitude);
SendBroadcast(broadcastIntent);
It is working very well, i can catch it in another class or any other other class as i want. In the Main activity i can't catch this broadcast message because the class needs to extends with "BroadcastReceiver" and the Main class already extends the "Activity". Ok then i created a class that extends with "BroadcastReceiver". And then i send this position back to the Main with an Event. On the Main class i get this position and add in the listview. But the event is everytime null.
PositionListener
[BroadcastReceiver]
[IntentFilter (new[] {"NewCoordinate"})]
class PositionListener : BroadcastListener
{
public override void OnReceive(Context context, Intent intent)
{
if(intent.Action == "NewCoordinate")
{
RefreshPositionsEvent(intent.GetDoubleExtra("Latitude",0), intent.GetDoubleExtra("Longitude",0)); //NullReferenceException
}
}
}
Main ` PositionAdapter positionAdapter; ... PositionListener posListener = new PositionListener(); posListener.RefreshPositionsEvent += () => { RunOnUiThread(() => { positionAdapter.NotifyDataSetChanged(); }); }
positionAdapter = new PositionAdapter(this, Resource.Layout.PositionListItem, posListener.Positions); lvPositions.Adapter = positionAdapter; `
I know that the positionAdapter is null when i initialize the posListener instance but when i want to initialize the positionAdapter first, the posListener's Positions property is not available yet.
Strange situation, but i don't know the proper way to connect the posListener with positionAdapter. They need eachother. The posListener needs to notify the positionAdapter to data set changed but the positionListener needs the posListener's Positions too.
Anyway. I got NullReferenceException when i call the event in the PositionListener class.
posListener.RefreshPositionsEvent += () =>
On this row the debug prompt always green, not yellow as normally. The debugger stay on this row when i want to step to the next row more time with green color. I think this is a sign.