Webview: Only the original thread that created a view hierarchy can touch its views.

You must Login before you can answer or comment on any questions.

My simple app with just a webview crashes with a CalledFromWrongThreadException. It is just a button that fires an event onclick to titanium. Titanium "natively" fires an event with a response message that is written to a div delement in the page.

This works fine but... the app "mostly" crashes, especially when the keyboard ist still open when the button is clicked.

What am I doing wrong? Any ideas welcome!

Info&start of stacktrace

Application type: mobile
Titanium SDK: 2.0.1 (04/12/12 16:33 999c68a) <from log, console empty at launch!> 
Platform & version: Android 2.2
Device: Android emulator AND physical device model
Host Operating System: Windows 7
Titanium Studio: 2.1.0.201205041150 
 
[ERROR][TiApplication(  267)] (KrollRuntimeThread) [43669,72437] Sending event: exception on thread: KrollRuntimeThread msg:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.; Titanium 2.0.1,2012/04/12 16:36,999c68a
[ERROR][TiApplication(  267)] android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
ApplicationWindow() in Titanium,
function ApplicationWindow() {
    //create component instance
    var self = Ti.UI.createWindow({
        backgroundColor:'#ffffff',
        navBarHidden:true,
        exitOnClose:true
    });
 
    //construct UI
    var webView = Ti.UI.createWebView({url:"../page/index.html"});
 
    self.add(webView);
 
    Ti.App.addEventListener('performSearch', function(evt) {
        Ti.App.fireEvent('searchResult', {message:"Searching for "+evt.data+": No result."});
        return false;
    });
 
    return self;
}
 
//make constructor function the public component interface
module.exports = ApplicationWindow;
html/js in Webview
<html>
<head>
<script type="text/javascript">
function performSearch() {
    Titanium.App.fireEvent('performSearch', 
        {data : escape(document.getElementById("searchpattern").value) }
    );
    return false;
};
 
Titanium.App.addEventListener('searchResult',function(e) {
    document.getElementById("result").innerHTML =  'Titanium says: ' + e.message;
    return false;
});
</script>
</head>
<body>
<form>
    <input id="searchpattern" type="TEXT" value="" />
    <button onClick="return performSearch();">Search</button>
    <div id="result"></div>
</form>
</body>
</html>

— asked 1 year ago by Jochen Renner
1 Comment
  • I'm receiving this error as well on Android, no problem on iOS, I'm doing basically the exactly same thing, sending some data from webview in Ti.App.fireEvent, and getting some back in an event listener.

    I do this in a few different situations, and sometimes it works, sometimes it doesn't. I'm thinking it has to do with data from the event being read only out of the webview context. I'm doing something similar where I send data to the main view, use it then send the same data back in the second event.

    — commented 1 year ago by Jordan Smith

3 Answers

Same here. I am getting this error when getting the HTML property of a WebView. I really don't know how to get around this problem.

Your Answer

Think you can help? Login to answer this question!