[mono-android] Problem with RunOnUiThread
BernhardGessler at cs-ag.de
BernhardGessler at cs-ag.de
Mon Mar 21 11:16:15 EDT 2011
Hi,
in my app i run a backgroundthread according to the example on the
monodroid homepage:
http://mono-android.net/Documentation/Guides/Writing_Responsive_Applications
And i keep getting this error when repeatedly triggering events to the gui
to display log-information:
03-21 15:25:03.100 6101 6101 I CSMOBILE: 15:25:03 Information: 10
Änderungen vom Server erhalten
03-21 15:25:03.108 6101 6101 I CSMOBILE: 161
03-21 15:25:03.358 6101 6101 I CSMOBILE: 15:25:03 Information: 10
Änderungen vom Server erhalten
03-21 15:25:03.366 6101 6101 I CSMOBILE: 162
03-21 15:25:03.475 6101 6101 I CSMOBILE: 15:25:03 Information: 10
Änderungen vom Server erhalten
03-21 15:25:03.530 6101 6145 D dalvikvm: GC_EXPLICIT freed 337 objects /
180632 bytes in 31ms
03-21 15:25:03.538 6101 6101 I CSMOBILE: 163
03-21 15:25:03.561 6101 6101 I MonoDroid: UNHANDLED EXCEPTION:
System.NullReferenceException: Object reference not set to an instance of
an object
03-21 15:25:03.561 6101 6101 I MonoDroid: at
Java.Lang.Thread/RunnableImplementor.Run () <0x000c4>
03-21 15:25:03.561 6101 6101 I MonoDroid: at
Java.Lang.IRunnableAdapter.n_Run (intptr,intptr) <0x00037>
03-21 15:25:03.561 6101 6101 I MonoDroid: at (wrapper dynamic-method)
object.7ff79b2e-d6ec-474f-aeea-a56a20c8aa9d (intptr,intptr) <0x0002b>
03-21 15:25:03.639 6101 6101 E mono :
03-21 15:25:03.639 6101 6101 E mono : Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of
an object
03-21 15:25:03.639 6101 6101 E mono : at
Java.Lang.Thread+RunnableImplementor.Run () [0x00000] in <filename
unknown>:0
03-21 15:25:03.639 6101 6101 E mono : at
Java.Lang.IRunnableAdapter.n_Run (IntPtr jnienv, IntPtr native__this)
[0x00000] in <filename unknown>:0
03-21 15:25:03.639 6101 6101 E mono : at (wrapper dynamic-method)
object:7ff79b2e-d6ec-474f-aeea-a56a20c8aa9d (intptr,intptr)
03-21 15:25:03.671 1325 1478 I ActivityManager: Process CSMobile.UI (pid
6101) has died.
03-21 15:25:03.678 1325 1464 I WindowManager: WIN DEATH: Window{45c85108
CSMobile.UI/csmobile.ui.forms.SyncOptions paused=false}
03-21 15:25:03.678 1325 1460 I WindowManager: WIN DEATH: Window{45efe2b0
CSMobile.UI/csmobile.ui.forms.Login paused=false}
03-21 15:25:03.678 1325 1522 I WindowManager: WIN DEATH: Window{45efeb88
CSMobile.UI/csmobile.ui.forms.Sync paused=false}
03-21 15:25:03.686 1325 1478 I ActivityManager: Start proc CSMobile.UI
for activity CSMobile.UI/csmobile.ui.forms.SyncOptions: pid=6146 uid=10094
gids={3003}
03-21 15:25:03.694 1139 1139 D Zygote : Process 6101 exited cleanly (1)
The whole situation works for about one to two minutes before the error
occurs. Events are triggered 2 to 5 times per second.
My backgroundthread passes Information to my Log-Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Genesis.Sync.ClientLib
{
public enum LogType
{
Information = 1,
Warning = 2,
Error = 3
}
public class Log
{
public delegate void LogAddedEventHandler(string message);
public static event LogAddedEventHandler LogAdded;
public static void WriteLog(string logMessage)
{
WriteLog(LogType.Information, logMessage);
}
public static void WriteLog(Exception error)
{
WriteLog(LogType.Error, error.Message + Environment.NewLine +
"Stacktrace: " + error.StackTrace);
if (error.InnerException != null)
{
WriteLog(LogType.Error, "Inner Exception: folgt");
WriteLog(error.InnerException);
}
}
public static void WriteLog(LogType type, string logMessage)
{
if (type >= Settings.LogLevel)
{
if (LogAdded != null)
{
LogAdded.Invoke(type + ": " + logMessage);
}
}
}
}
}
My BussinessLogic listens to the Log and triggers another event to send
its status to the gui:
public Sync_Logic()
{
Log.LogAdded += (message) =>
{
if (this.LogAdded != null)
{
this.LogAdded(message);
}
};
}
My Gui handles the event like this:
this._logic.LogAdded += (message) =>
{
this.RunOnUiThread(() =>
{
Log.Info("CSMOBILE", DateTime.Now.ToShortTimeString()
+ ":" + DateTime.Now.Second.ToString("D2") + " " + message);
this._textLog = FindViewById<TextView>(Resource.Id
.sync_textLog);
this._textLog.Text = DateTime.Now.ToShortTimeString()
+ ":" + DateTime.Now.Second.ToString("D2") + " " + message + Environment
.NewLine + this._textLog.Text;
Log.Info("CSMOBILE", (count++).ToString());
});
};
Adding try-catch didn't help:
this._logic.LogAdded += (message) =>
{
this.RunOnUiThread(() =>
{
try
{
Log.Info("CSMOBILE", DateTime
.Now.ToShortTimeString() + ":" + DateTime.Now.Second.ToString("D2") + " "
+ message);
this._textLog = FindViewById<TextView>(Resource.Id
.sync_textLog);
this._textLog.Text = DateTime
.Now.ToShortTimeString() + ":" + DateTime.Now.Second.ToString("D2") + " "
+ message + Environment.NewLine + this._textLog.Text;
Log.Info("CSMOBILE", (count++).ToString());
}
catch (Exception)
{
Log.Info("CSMOBILE", "Fehler bei Callback");
}
});
};
But if i remove the 2 lines filling the textView no error appears:
this._logic.LogAdded += (message) =>
{
this.RunOnUiThread(() =>
{
Log.Info("CSMOBILE", DateTime.Now.ToShortTimeString()
+ ":" + DateTime.Now.Second.ToString("D2") + " " + message);
Log.Info("CSMOBILE", (count++).ToString());
});
};
for the last few days I've tried everything i could imagine. But i wasn't
able to resolve the problem. Would be great if anyone had a hint for me.
Thanks for your help in advance
Bernhard Geßler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110321/03d06434/attachment-0001.html
More information about the Monodroid
mailing list