Calling View methods on another thread than the UI thread.错误
来源:互联网 发布:php中pre 编辑:程序博客网 时间:2024/05/17 08:58
今天开发中有一项需求是webView显示列表数据并分享到新浪 微信 朋友圈等。
分享接口是调用原生接口的:
/** * 分享 * * @param sns_Json 分享SnsShareEntity实体 --> json格式 */ @JavascriptInterface public void shareGood(String sns_Json) { Logger.e("分享 :"+sns_Json); final SnsShareEntity snsShareEntity = GsonUtil.GetFromJson(sns_Json, SnsShareEntity.class); if (snsShareEntity != null) { //调用Umeng的分享接口 initShare(); ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, "")); } }
webView中直接调用shareGood方法,分享到微信 微信朋友圈、QQ都是没有问题,唯独分享到新浪微博的是报错:
根据提示和结合自己的项目分析:
webview调用shareGood方法,本身是属于webview里js调用,不属于UiThread;
当分享新浪微博的时候,新浪微博是打开一个网页登录界面的。所以需要运行在UiThread中,导致报错。
最后在stackoverflow中搜索出一个解决方案:
java.lang.IllegalStateException: Calling View methods on another thread than the UI thread
runOnUiThread(new Runnable() { @Override public void run() { // Code for WebView goes here }});// This code is BAD and will block the UI threadwebView.loadUrl("javascript:fn()");while(result == null) { Thread.sleep(100);}
所以最后我自己修改了shareGood方法,如下:
/** * 分享 * * @param sns_Json 分享SnsShareEntity实体 --> json格式 */ @JavascriptInterface public void shareGood(String sns_Json) { Logger.e("分享 :"+sns_Json); final SnsShareEntity snsShareEntity = GsonUtil.GetFromJson(sns_Json, SnsShareEntity.class); if (snsShareEntity != null) { //解决Calling View methods on another thread than the UI thread.错误// topBar.postDelayed(new Runnable() {// @Override// public void run() {// // Code for WebView goes here// initShare();// ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, ""));// }// },1000); //解决Calling View methods on another thread than the UI thread.错误 //http://stackoverflow.com/questions/20255920/java-lang-illegalstateexception-calling-view-methods-on-another-thread-than-the runOnUiThread(new Runnable() { @Override public void run() { // Code for WebView goes here initShare(); ShareUtils.sendVideoShare(context, topBar, new GoodShareInterFace(mController, snsShareEntity, context, "")); } }); } }
最后解决了问题,记录下笔记,希望能帮助到其他朋友!
2 0
- Calling View methods on another thread than the UI thread.错误
- 关于Calling View methods on another thread than the UI thread的错误
- 关于Calling View methods on another thread than the UI thread的错误
- 解决java.lang.IllegalStateException: Calling View methods on another thread than the UI thread
- 【已解决】Calling View methods on another thread than the UI thread.
- Solved this program:accessed from a thread other than the thread it was created on.
- WebView methods on same thread error
- you are calling a UIKit method that can only be invoked from the UI thread.
- [Android]All WebView methods must be called on the same thread.线程安全
- android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]
- java.lang.Throwable: Warning: A WebView methods must be called on the same thread.
- Cause by:All WebView methods must be called on the same thread[问题已解决]
- 安卓调用JS All WebView methods must be called on the same thread
- 异常A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same
- A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same t
- Main Thread Checker: UI API called on a background thread:
- Main Thread & UI Thread
- UI Thread & Worker Thread
- 实现一个简单的数学计算题
- QT中的布局类
- java中的基本数据类型
- 趣味编程__正弦和线性函数的焦点
- 杭电1200
- Calling View methods on another thread than the UI thread.错误
- gridview+adapter
- 关于程序猿的一点看法
- 解决外边距折叠问题
- 寄存器与内存的区别
- react native android使用react-native-tab-navigator来做底部导航tabbar(1)
- 巧用/用足 UpdateAllViews()中的三个参数
- HDOJ 2084 数塔-简单DP
- git如何提交代码