Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25382] Android: Wrong thread error in DrawerLayout when not running on main thread

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2017-11-13T18:55:19.000+0000
Affected Version/sn/a
Fix Version/sRelease 7.0.0
ComponentsAndroid
Labels6.2.0, android, drawerlayout
ReporterMichael Gangolf
AssigneeGary Mathews
Created2017-09-14T18:25:34.000+0000
Updated2018-06-11T14:15:25.000+0000

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

Comments

  1. Gary Mathews 2017-10-11

    master: https://github.com/appcelerator/titanium_mobile/pull/9516
  2. Lokesh Choudhary 2017-11-11

    FR Passed. Waiting for merge to get enabled.
  3. Lokesh Choudhary 2017-11-13

    PR Merged.
  4. Abir Mukherjee 2017-11-15

    Changes are seen in SDK 7.0.0.v20171114202841.
  5. Guile 2018-06-11

    I still have the problem with SDK7.1 and run on main thread false. I don't want to set it to true, because the last time I did it, there was a lot of buggy things. Can you describe the solution that was made, and how I can prevent this bug to occur ? Thank you

JSON Source