---
title: "BlackBerry 异常UI engine accessed without holding the event "
author: "REALGODO"
date: "2012-07-24T15:01:51Z"
tags: []
categories: ["移动开发"]
slug: "blackberry-ui-engine-accessed-without-holding-the-event"
draft: false
lastmod: "2012-07-24T15:02:18Z"
id: 1605604
url: "https://realgodo.iteye.com/blog/1605604"
---

BB开发中发现了一个异常UI engine accessed without holding the event ，代码的大体结构式 一个Screen 注册到网络模块（另外一个线程）上，到网络状态发生变化，监听通知 Screen 进行UI上的变化 。

 

后来去网上查了一下，发现原来是由于BlackBerry的UI架构中避免多线程竞争等情况的一个机制引起的异常。BlackBerry中限定在同一时间内，只能有一个线程对UI执行更新操作，并且这个线程默认就是UiApplication子类的主线程。而其它线程如果直接试图修改UI，就会引起这个异常。

 

解决方法就是加入synchronized (UiApplication.getEventLock()){}代码块。

```
	
	public void onConnectSuccess() {
		synchronized (UiApplication.getEventLock()){
			boolean isLogin = RmsFacade.getChars(RmsSeed.ENCODEKEY)!=null;
			if(isLogin){
				Intent intent = getIntent();
				intent.setToScreen(TabScreen.class);
				intent.startScreen();
				ScreenFactory.getSingleInstance().screenFinish(WelcomeScreen.class);
			}else{
				Intent intent = getIntent();
				intent.setToScreen(RegistScreen.class);
				intent.startScreen();
				ScreenFactory.getSingleInstance().screenFinish(WelcomeScreen.class);
				BlzContext.getSingleInstance().netStatListener = null;
			}

		}
	}
```