Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-25382

Android: Wrong thread error in DrawerLayout when not running on main thread

    Details

    • Story Points:
      2
    • Sprint:
      2017 Sprint 21 SDK, 2017 Sprint 22 SDK, 2017 Sprint 23 SDK

      Description

      When using a DrawerLayout in an app that is not running with
      <property name="run-on-main-thread" type="bool">true</property> it will raise a "CalledFromWrongThreadException" error:

      Code

      var win = Ti.UI.createWindow(),
      	leftView = Ti.UI.createView({
      		backgroundColor: "#333",
      		layout: "vertical"
      	}),
      	centerView = Ti.UI.createView({
      		backgroundColor: "#fff"
      	}),
      	rightView = Ti.UI.createView({
      		backgroundColor: "#333"
      	}),
      	drawer = Ti.UI.Android.createDrawerLayout({
      		leftView: leftView,
      		centerView: centerView,
      		rightView: rightView
      	}),
      	btn1 = Ti.UI.createButton({
      		top: 100,
      		title: 'Left'
      	}),
      	btn2 = Ti.UI.createButton({
      		top: 150,
      		title: 'Right'
      	});
       
      for (var i = 0; i < 10; ++i) {
      	var lbl1 = Ti.UI.createLabel({
      		color: "#fff",
      		text: "Item",
      		top: 10,
      		bottom: 10,
      		height: Ti.UI.SIZE
      	});
      	leftView.add(lbl1);
      }
       
      btn2.addEventListener('click', function() {
      	drawer.toggleRight();
      });
      btn1.addEventListener('click', function() {
      	drawer.toggleLeft();
      });
       
      centerView.add(btn1);
      centerView.add(btn2);
       
      win.addEventListener('open', function() {
      	var activity = win.getActivity(),
      		actionbar = activity.getActionBar();
       
      	if (actionbar) {
      		actionbar.displayHomeAsUp = true;
      		actionbar.onHomeIconItemSelected = function() {
      			drawer.toggleLeft();
      		};
      	}
       
      });
       
      win.add(drawer);
      win.open();
      

      KitchenSink Demo: https://github.com/appcelerator/kitchensink-v2/pull/14

      Error:

      [WARN]  W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
      [WARN]  W/System.err:   at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7107)
      [WARN]  W/System.err:   at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1139)
      [WARN]  W/System.err:   at android.view.ViewGroup.invalidateChild(ViewGroup.java:5254)
      [WARN]  W/System.err:   at android.view.View.invalidateInternal(View.java:13669)
      [WARN]  W/System.err:   at android.view.View.invalidate(View.java:13633)
      [WARN]  W/System.err:   at android.view.View.invalidate(View.java:13617)
      [WARN]  W/System.err:   at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1615)
      [WARN]  W/System.err:   at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1641)
      [WARN]  W/System.err:   at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1625)
      [WARN]  W/System.err:   at ti.modules.titanium.ui.widget.TiUIDrawerLayout.openLeft(TiUIDrawerLayout.java:193)
      [WARN]  W/System.err:   at ti.modules.titanium.ui.widget.TiUIDrawerLayout.toggleLeft(TiUIDrawerLayout.java:188)
      [WARN]  W/System.err:   at ti.modules.titanium.ui.android.DrawerLayoutProxy.toggleLeft(DrawerLayoutProxy.java:49)
      [WARN]  W/System.err:   at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method)
      [WARN]  W/System.err:   at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57)
      [WARN]  W/System.err:   at org.appcelerator.kroll.runtime.v8.V8Function.handleMessage(V8Function.java:82)
      [WARN]  W/System.err:   at android.os.Handler.dispatchMessage(Handler.java:98)
      [WARN]  W/System.err:   at android.os.Looper.loop(Looper.java:173)
      [WARN]  W/System.err:   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:118)
      

      Workaround

      Add <property name="run-on-main-thread" type="bool">true</property> to tiapp.xml

      Environment
      Ti SDK 6.2.0.GA
      Android 7

        Attachments

          Activity

            People

            • Assignee:
              gmathews Gary Mathews
              Reporter:
              michael Michael Gangolf
              Reviewer:
              Lokesh Choudhary
              Tester:
              Lokesh Choudhary
            • Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Backbone Issue Sync

                • Titanium SDK/CLI <> Titanium Mobile
                  Synced with:
                  TIMOB-5869
                  Sync status:
                  ERROR
                  Last received:
                  Last sent:

                  Git Source Code