设为首页 加入收藏

TOP

Android里子线程真的不能刷新UI吗?
2014-11-24 12:42:41 来源: 作者: 【 】 浏览:1
Tags:Android 里子 线程 真的 不能 刷新

让我们一起看看代码吧!


首先,CalledFromWrongThreadException这个异常是有下面的代码抛出的:


void checkThread() {


if (mThread != Thread.currentThread()) {


throw new CalledFromWrongThreadException(


"Only the original thread that created a view hierarchy can touch its views.");


}


}


该段代码出自 framework/base/core/java/android/view/ViewRoot.java


其次,看看RootView的构造函数:


public ViewRoot(Context context) {


super();



if (MEASURE_LATENCY && lt == null) {


lt = new LatencyTimer(100, 1000);


}



// For debug only


//++sInstanceCount;



// Initialize the statics when this class is first instantiated. This is


// done here instead of in the static block because Zygote does not


// allow the spawning of threads.


getWindowSession(context.getMainLooper());



mThread = Thread.currentThread();


mLocation = new WindowLeaked(null);


mLocation.fillInStackTrace();


mWidth = -1;


mHeight = -1;


mDirty = new Rect();


mTempRect = new Rect();


mVisRect = new Rect();


mWinFrame = new Rect();


mWindow = new W(this, context);


mInputMethodCallback = new InputMethodCallback(this);


mViewVisibility = View.GONE;


mTransparentRegion = new Region();


mPreviousTransparentRegion = new Region();


mFirst = true; // true for the first time the view is added


mAdded = false;


mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);


mViewConfiguration = ViewConfiguration.get(context);


mDensity = context.getResources().getDisplayMetrics().densityDpi;


}


最后,我们看看ViewRoot.checkThread的调用顺序:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇android开发之读取xml文件 下一篇Android子线程在没有ViewRoot的情..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)