21 เมษายน 2551

ค่า defaultButtonFlag ใน Alert.show()

วันนี้ได้ทำการเขียนโปรแกรมเพื่อสอบถาม user ว่าต้องการให้ save งานหรือไม่ ทำให้ต้องใช้งาน Alert ซึ่งได้ลองดูในเอกสารวิธีการใช้งาน และคัดลองมาจากตัวอย่างเลย

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;

// Event handler function uses a static method to show
// a pop-up window with the title, message, and requested buttons.
private function clickHandler(event:Event):void {
Alert.show("Do you want to save your changes?", "Save Changes", 3, this, alertClickHandler);
}

// Event handler function for displaying the selected Alert button.
private function alertClickHandler(event:CloseEvent):void {
if (event.detail==Alert.YES)
status.text="You answered Yes";
else
status.text="You answered No";
}

// Event handler function changes the default Button labels and sets the
// Button widths. If you later use an Alert with the default Buttons,
// you must reset these values.
private function secondClickHandler(event:Event):void {
Alert.buttonWidth = 100;
Alert.yesLabel = "Magenta";
Alert.noLabel = "Blue";
Alert.cancelLabel = "Green";

Alert.show("Select a color:","Color Selection",1|2|8,this);
}
]]>
</mx:Script>

<mx:Panel title="Alert Control Example" width="75%" horizontalAlign="center" paddingTop="10">
<mx:Text width="100%" color="blue" textAlign="center"
text="Click the button below to display a simple Alert window."/>
<mx:Button label="Click Me" click="Alert.show(&apos;Hello World!&apos;, &apos;Message&apos;);"/>

<mx:Text width="100%" color="blue" textAlign="center"
text="Click the button below to display an Alert window and capture the button pressed by the user."/>
<mx:Button label="Click Me" click="clickHandler(event);"/>
<mx:Label id="status" fontWeight="bold"/>

<mx:Text width="100%" color="blue" textAlign="center"
text="Click the button below to display an Alert window that uses custom Button labels."/>
<mx:Button label="Click Me" click="secondClickHandler(event);"/>
</mx:Panel>

</mx:Application>


ผลปรากฎว่าเมื่อทำการ run แล้วเกิด runtime error ดังนี้

TypeError: Error #2007: Parameter source must be non-null.
at flash.accessibility::Accessibility$/sendEvent()
at mx.accessibility::AlertAccImpl/eventHandler()[C:\dev\flex_201_gmc\sdk\frameworks\mx\accessibility\AlertAccImpl.as:243]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent.as:8323]
at mx.core::UIComponent/set initialized()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent.as:1095]
at mx.managers::LayoutManager/doPhasedInstantiation()[C:\dev\flex_201_gmc\sdk\frameworks\mx\managers\LayoutManager.as:696]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent.as:7909]
at mx.core::UIComponent/callLaterDispatcher()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent.as:7852]


ก็เลยงงๆ ว่าที่มีปัญหาบอกว่ามีค่าเป็น null นั้นคืออะไร เลยลอง debug ดู เล็งไปที่ตัวแปร parent ว่าที่ส่งไปให้มันเป็น null หรือไม่ ซึ่งผลปรากฎว่าไม่ใช่ มีค่าเป็น Container ถูกต้อง เลยปวดหัวไปใหญ่ เลยค่อยๆ ใส่ parameter ไปทีละตัว ไปหยุดอยู่ที่ flags ซึ่งลองกำหนดเป็น (Alert.YES | Alert.NO) แล้วลองกลับไปดูค่า default ของ defaultButtonFlag ซึ่งมีค่าเป็น 4 หรือตรงกับปุ่ม Alert.OK จึงทำให้เจอปัญหาว่า คนเขียน Alert class ไม่มีการตรวจสอบว่า default button นั้นกำหนดมาถูกต้องหรือไม่ อันที่จริงน่าจะไปกำหนดใน flag เหมือนใน VB น่าจะเข้าท่ากว่า หรือไม่ควรจะ throw ความผิดพลาดออกมาว่ากำหนด default ไม่ถูกต้อง

เมื่อแก้ไขโปรแกรมตัวอย่าง โดยกำหนด defaultButtonFlag ใหม่แล้ว โปรแกรมสามารถทำงานได้ถูกต้อง

Alert.show("Do you want to save your changes?", "Save Changes", 3, this, alertClickHandler, null, Alert.NO);

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

เกี่ยวกับเจ้าของบล๊อก