[mono-android] Antwort: Re: Problem with RunOnUiThread + Error "giving up"

BernhardGessler at cs-ag.de BernhardGessler at cs-ag.de
Wed Mar 30 19:13:15 EDT 2011


Hi,

i've made some progress here. My Original Problem was that while a long 
enduring task i regularly (2-5 times per second) updated my UI like this:

                this._logic.LogAdded += (message) =>
            {
                this.RunOnUiThread(() =>
                {
                    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;
                });
            };

i got the following error:

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.

Adding try/catch didn't help.
I then limited the Textlength in  in my TextView via 

           string log = this._textLog.Text;
                        if (log.Length > 1000)
                        {
                            log = log.Substring(0, 999);
                        }
                        this._textLog.Text = log;

Since then I got no more NullReferenceExceptions. But now i'm getting a 
new error:

03-31 00:23:33.939  4067  4067 I dalvikvm: "main" prio=5 tid=1 RUNNABLE
03-31 00:23:33.939  4067  4067 I dalvikvm:   | group="main" sCount=0 
dsCount=0 s=N obj=0x40020990 self=0xce50
03-31 00:23:33.939  4067  4067 I dalvikvm:   | sysTid=4067 nice=0 
sched=0/0 cgrp=default handle=-1345017744
03-31 00:23:33.939  4067  4067 I dalvikvm:   | schedstat=( 10213165196 
4036560128 7403 )
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
java.lang.Runtime.gc(Native Method)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
java.lang.System.gc(System.java:246)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
mono.java.lang.IRunnableAdapter.n_run(Native Method)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
mono.java.lang.IRunnableAdapter.run(IRunnableAdapter.java:9)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Handler.handleCallback(Handler.java:587)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Handler.dispatchMessage(Handler.java:92)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Looper.loop(Looper.java:143)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.app.ActivityThread.main(ActivityThread.java:4717)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
java.lang.reflect.Method.invokeNative(Native Method)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
java.lang.reflect.Method.invoke(Method.java:521)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
dalvik.system.NativeStart.main(Native Method)
03-31 00:23:33.939  4067  4067 I dalvikvm: 
03-31 00:23:33.939  4067  4067 I dalvikvm: "Thread-42" prio=5 tid=8 
RUNNABLE JIT
03-31 00:23:33.939  4067  4067 I dalvikvm:   | group="main" sCount=1 
dsCount=0 s=N obj=0x45bd8620 self=0x690a30
03-31 00:23:33.939  4067  4067 I dalvikvm:   | sysTid=4119 nice=0 
sched=0/0 cgrp=default handle=4259048
03-31 00:23:33.939  4067  4067 I dalvikvm:   | schedstat=( 652404783 
111968994 259 )
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.MessageQueue.enqueueMessage(MessageQueue.java:~208)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Handler.sendMessageAtTime(Handler.java:457)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Handler.sendMessageDelayed(Handler.java:430)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.os.Handler.post(Handler.java:248)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
android.app.Activity.runOnUiThread(Activity.java:3724)
03-31 00:23:33.939  4067  4067 I dalvikvm:   at 
dalvik.system.NativeStart.run(Native Method)

.
.
. (continues like this)

03-31 00:23:39.983  4067  4067 E dalvikvm: threadid=1: stuck on 
threadid=8, giving up
03-31 00:23:39.983  4067  4067 D dalvikvm: threadid=1: sending two 
SIGSTKFLTs to threadid=8 (tid=4119) to cause debuggerd dump
03-31 00:23:41.987  4067  4067 D dalvikvm: Sent, pausing to let debuggerd 
run
03-31 00:23:49.990  4067  4067 D dalvikvm: Continuing
03-31 00:23:49.990  4067  4067 E dalvikvm: VM aborting
03-31 00:23:50.022  1321  1321 I ActivityManager: Process CSMobile.UI (pid 
4067) has died.


If I comment out the whole RunOnUiThread no error appears.

I've managed to build a testscenario that throws both types of errors:

    public class Activity1 : Activity
    {
        TextView textview;
        private const int testCase = 0;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            textview = new TextView(this);
            textview.Text = "Working..";
            SetContentView(textview);

            ThreadPool.QueueUserWorkItem(o => MemUsage());
        }

        void MemUsage()
        {
            List<string> l = new List<string>();

            // Simulating Heavy Memory Usage
            for (int i = 0; i < 1000000; i++)
            {
                string h = "Hello";
                string w = "World";

                int a = i + 2;

                l.Add(h + w + a);

                Log.Info("BUGTEST", i.ToString());

                if (i % 100 == 0) RunOnUiThread(() =>
                {
                    string log = textview.Text;

                    if (testCase == 1)
                    {
                        if (log.Length > 1000)
                        {
                            log = log.Substring(0, 999);
                        }
                    }

                    textview.Text = h + w + a + System.Environment.NewLine 
+ log;
                });
            }
        }

        public override void OnLowMemory()
        {
            Log.Info("BUGTEST", "ON Low Memory");
        }
    }

Change testCase to 1 to get the second type of error. It doesn't seem to 
be a problem of low memory as the logentry in OnLowMemory() in never 
written.


Thanks for your help in advance

Bernhard Geßler





Jonathan Pryor <jpryor at novell.com> 
Gesendet von: monodroid-bounces at lists.ximian.com
21.03.2011 16:43
Bitte antworten an
monodroid at lists.ximian.com


An
monodroid at lists.ximian.com
Kopie
HagenBackhaus at cs-ag.de
Thema
Re: [mono-android] Problem with RunOnUiThread






On Mar 21, 2011, at 11:16 AM, BernhardGessler at cs-ag.de wrote:
> in my app i run a backgroundthread according to the example on the 
monodroid homepage: 
> 
http://mono-android.net/Documentation/Guides/Writing_Responsive_Applications 


Please file a complete test case so that we can investigate further.

 - Jon

_______________________________________________
Monodroid mailing list
Monodroid at lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110331/b7b2c561/attachment-0001.html 


More information about the Monodroid mailing list