[TIMOB-25382] Android: Wrong thread error in DrawerLayout when not running on main thread
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2017-11-13T18:55:19.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 7.0.0 |
Components | Android |
Labels | 6.2.0, android, drawerlayout |
Reporter | Michael Gangolf |
Assignee | Gary Mathews |
Created | 2017-09-14T18:25:34.000+0000 |
Updated | 2018-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
master: https://github.com/appcelerator/titanium_mobile/pull/9516
FR Passed. Waiting for merge to get enabled.
PR Merged.
Changes are seen in SDK 7.0.0.v20171114202841.
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