I have webview, and I’m using geo-location feature in android.
I’m using javascript (onload) to call Android method
public void getLocation() {
_context.getLocation();
}
and when location recieved this execute
public void locationUpdated(Location location) {
NumberFormat frm = NumberFormat.getNumberInstance(new Locale("en_US"));
// call javascript function
webView.loadUrl("javascript:locationChangedHandler(" + frm.format (location.getLatitude()) + "," + frm.format(location.getLongitude()) + ")");
}
Sometime I got this exception, and this exception
04-04 01:17:06.262: ERROR/AndroidRuntime(659): FATAL EXCEPTION: Timer-1
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
at android.view.View.invalidate(View.java:5139)
at android.view.View.onFocusChanged(View.java:2664)
at android.widget.TextView.onFocusChanged(TextView.java:6469)
at android.widget.AutoCompleteTextView.onFocusChanged(AutoCompleteTextView.java:1048)
at android.webkit.WebTextView.onFocusChanged(WebTextView.java:357)
at android.view.View.clearFocusForRemoval(View.java:2577)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2188)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:2181)
at android.view.ViewGroup.removeView(ViewGroup.java:2129)
at android.webkit.WebTextView.remove(WebTextView.java:583)
at android.webkit.WebView.clearTextEntry(WebView.java:1830)
at android.webkit.WebView.loadUrl(WebView.java:1542)
at android.webkit.WebView.loadUrl(WebView.java:1553)
at com.binus.MainView$1.gotLocation(MainView.java:33)
at com.binus.GeoLocation$GetLastLocation.run(GeoLocation.java:88)
at java.util.Timer$TimerImpl.run(Timer.java:289)
Is this something todo with accessing UI thread from other thread ?
Is calling Android (Java) method from Javascript execute on non UI thread ?
And how to make my code thread safe ?
I’m not sure how you got off the UI thread, but to get back on you can use
post:if you’re inside a
View. If you’re in anActivitythen changeposttorunOnUiThread.