1
我有webview,我在android中使用地理位置功能。 我使用JavaScript(onload事件)來調用Android的方法從Android調用Android方法Android'CalledFromWrongThreadException'
public void getLocation() {
_context.getLocation();
}
當位置收到此執行
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()) + ")");
}
有時我得到這個例外,這個例外
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)
是這是從其他線程訪問UI線程的東西嗎? 正在從非UI線程調用Android(Java)方法執行嗎?
以及如何使我的代碼線程安全?
謝謝你的工作,:D。 – F8R 2011-04-04 01:41:43
UPVOTE bigtime !!!!多謝 – Arnold 2011-12-12 14:05:19