本文介绍了Scroller 类中的焦点管理器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scroller.as 类第 2139 行中,我收到以下错误:

In the Scroller.as class line 2139 I'm getting the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at spark.components::Scroller/focusInHandler()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Scroller.as:2139]
    at flash.display::Stage/set focus()

来自 Scroller.as

From Scroller.as

/**
 *  @private 
 *  Listens for any focusIn events from descendants 
 */ 
override protected function focusInHandler(event:FocusEvent):void
{
    super.focusInHandler(event);

    // When we gain focus, make sure the focused element is visible
    if (viewport && ensureElementIsVisibleForSoftKeyboard)
    {
        var elt:IVisualElement = focusManager.getFocus() as IVisualElement; 
        lastFocusedElement = elt;
    }
}

既然这是框架代码,我有什么办法来阻止它?

Since this is framework code what option do I have to prevent it?

背景
我创建了一个弹出的 TitleWindow,在其中添加了一个模块并显示它.Module有几个States,每个State是一个Group,一个Group有一个List,那个List有一个ItemRenderer,那个ItemRenderer有一个Checkbox.

Context
I have created a pop up TitleWindow, added a Module in it and displayed it. The Module has a few States, in each State is a Group, one Group has a List, that List has an ItemRenderer, that ItemRenderer has a Checkbox.

模块也有一个菜单.当菜单打开时,弹出的菜单会列出模块可用的状态.当从弹出的菜单中选择一个项目时,我会更改为另一种状态.

The Module also has a Menu. When the Menu is opened the menu pop up lists the states the Module has available. When an item is selected from the menu pop up I change to another state.

当状态改变并且最后一项是复选框时,就会产生错误.至少这是我认为正在发生的事情.我推断这是因为在 Scroller 类中,处理程序正在处理一个事件.在那个事件上是当前目标.当前目标是复选框.

When the state is changed and the last item is the checkbox then the error is generated. At least that's what I think is happening. I deduced this because in the Scroller class the handler is handling an event. On that event is the current target. That current target is the checkbox.

更新 - 重现步骤

// inside the Application.mxml
// define variables
public var popup:Group;
public var titleWindow:TitleWindow;


// shows pop up
public function showInspector():void {

    // inside show inspector method
    // create new inspector container
    popup = new InspectorContainer(); // a group implements="mx.managers.IFocusManagerContainer"
    titleWindow = new TitleWindow();
    titleWindow.addElement(popup);

    // display pop up title window
    PopUpManager.addPopUp(titleWindow, this, false);
}

<fx:Declarations>
    <modules:InspectorContainer/>
</fx:Declarations>
  1. 显示弹出窗口.弹出的是一个标题窗口,其中 InspectorContainer(一个组)作为第一个元素.

  1. Show pop up. The pop up is a Title Window with InspectorContainer (a group) as the first element.

在弹出窗口中从家庭状态(默认状态)更改为在线状态(当用户单击按钮时会发生这种情况).在线状态有一个列表.List 有一个 itemrenderer.itemrenderer 有一个复选框.选中复选框.到目前为止一切顺利.

In pop up change from home state (default state) to online state (this happens when the user clicks a button). The online state has a List. The List has an itemrenderer. The itemrenderer has a checkbox. Select the checkbox. So far so good.

弹出窗口 (InspectorContainer) 有一个 mx:MenuBar 实例.当您单击菜单栏中的项目时,菜单栏会显示一个菜单项.

The pop up (InspectorContainer) has a mx:MenuBar instance. When you click on an item in the menubar the menubar displays a menu items.

单击菜单列表中的项目.调用 itemClick menuHandler.在此功能中,弹出窗口会更改状态.

Click an item in the menu list. The itemClick menuHandler is called. In this function the pop up changes state.

这是发生错误的时候.

推荐答案

完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            autoDrawBackground="true"
            width="100%"
            implements="mx.managers.IFocusManagerContainer">

    <fx:Script>
    <![CDATA[
        public function get defaultButton():IFlexDisplayObject {
            return null;
        }

        public function set defaultButton(value:IFlexDisplayObject):void {
            // do nothing
        }

        override public function get systemManager():ISystemManager {
            return null;
        }
    ]]>
    </fx:Script>

    <!-- other stuff -->

    <s:CheckBox id="enabledCheckbox" />

</s:ItemRenderer>

解决方案是实现 IFocusManagerContainer 或基本上它需要的功能.在我的情况下,我必须在 Scroller focusInHandler 处理程序中覆盖焦点事件的目标对象(组件)的容器中的 systemManager getter.

The solution was to implement the IFocusManagerContainer or basically the function it needed. In my case I had to override the systemManager getter in the container of the target object (component) of the focus event in the Scroller focusInHandler handler.

我是这样想出来的.该错误发生在 Scroller focusInHandler 中.焦点事件(event.target)的目标属性是复选框(最后一个获得焦点的项目).Checkbox 的容器是我的 ItemRenderer.这是我实现 IFocusManagerContainer 接口的地方.我提到这一点是因为沿着这条路径有 5 个或更多的父级.

I figured it out this way. The error occurs in the Scroller focusInHandler. The target property of the focus event (event.target) is the Checkbox (the last item to have focus). The container of the Checkbox is my ItemRenderer. This is where I implemented the IFocusManagerContainer interface. I mention this because there are 5 or more parent levels along this path.

在我添加的 ItemRenderer 中:

In the ItemRenderer I added:

override public function get systemManager():ISystemManager {
    return null;
}

这个方法是你在实现mx.managers.IFocusManagerContainer接口时必须实现的方法.

This method is a method you have to implement when you implement the mx.managers.IFocusManagerContainer interface.

那个接口还要求我定义 defaultButton 的 getter 和 setter,但没有必要解决这个错误.在测试中,我删除了接口,删除了 defaultButton 成员并保留了 systemManager getter,仅此而已它仍然解决了问题.

That interface also required me to define the defaultButton getter and setter but it was not necessary to solve this error. In tests I removed the interface, removed the defaultButton members and left the systemManager getter and with just that it still solved the problem.

另外,感谢 noobsarepeople2 的帮助.

Also, thanks to noobsarepeople2 for help.

这篇关于Scroller 类中的焦点管理器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:18