Comunidad de diseño web y desarrollo en internet online

PROBLEMA al crear un popUp de custom componente con textInput

Citar            
MensajeEscrito el 04 Oct 2011 12:20 pm
Hola a tod@s,
Llevo mucho tiempo dandole vueltas para crear un popUp a partir de un custom component con un textInput. He conseguido crear popUp a partir de custom components de ChecksBox y rank sim problema alguno pero al crearlo de un textInput y ejecutarlo, al pulsar sobre el textInput para introducir texto me salta el dichoso error
TypeError: Error #1009: Cannot access a property or method of a null object reference.

He probado hacerlo en modo vista pero no me sirve y ya estoy desesperándome. He leido que en ocasiones se crea la referencia antes que el objeto y causa errores, no se si van por ahi los tiros. Encima del error no puedo sacar nada en claro...

La llamada desde la vista ppal

Código ActionScript :

public function getTexto():void {
    //crear el popup
     var textoVar: TextPicker = new TextPicker();
     textoVar.addEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextoSet);
     textoVar.addEventListener(TextPickerEvent.TEXT_PICKER_CANCEL, onTextoCancel);
     PopUpManager.addPopUp(textoVar, this, true);
}

private function onTextoSet(event:TextPickerEvent): void {
     datoUsuario1 = event.textValue as String;
     nodoEditado = nodoEncontrado.nodeName;
     onTextoCancel(event);
}

private function onTextoCancel(event:TextPickerEvent): void {
     var textoItemVar:TextPicker = event.target as TextPicker;
     textoItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextoSet);
     textoItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_CANCEL,onTextoCancel);
}

//TextPickerEvent.as
package components {
     import flash.events.Event;

     public class TextPickerEvent extends Event {
          public static const TEXT_PICKER_SET: String = 'textPickerSet';
          public static const TEXT_PICKER_CANCEL: String = 'textPickerCancel';
          public var textValue: String;

          public function TextPickerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) {
          super(type, bubbles, cancelable);
          }
     }
}

TextPicker.mxml

Código :

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" width="444" height="236">

<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;

[Bindable]
public var textValue:TextInput;

private function setText():void
{
   var dateObjects:SharedObject;
   var dpe1:TextPickerEvent = new TextPickerEvent(TextPickerEvent.TEXT_PICKER_SET);
   dpe1.textValue = userText.text;
   dispatchEvent(dpe1);

   PopUpManager.removePopUp(this);
   trace("** Valor introducido: " + userText.text);
   // Guardo en el directorio la fecha y hora introducida
   try{
      dateObjects = SharedObject.getLocal("datosGuardados");
   }
   catch(error:Error){
      trace("SharedObject Error: No existe el directorio y no se puede crear uno nuevo.");
      return;
   }
   dateObjects.data.text=userText.text;
   dateObjects.flush();
}

private function cancelText():void
{
   PopUpManager.removePopUp(this);
}
]]>
</fx:Script>

   <s:TextInput id="userText" x="1" y="71"  width="100%"/>
   <s:Button x="10" y="167" width="200" height="60" label="Aplicar" click="setText()" fontSize="20"/>
   <s:Button x="237" y="170" width="200" height="60" label="Cancelar" click="cancelText()" fontSize="20"/>
</s:Group>


Me estoy volviendo loco :shock: y agradecería infinitamente vuestra ayuda, muchas gracias por vuestro tiempo y espero que alguien pueda ayudarme,
saludos
Alvaro

Por alvaroqp85

7 de clabLevel



 

firefox
Citar            
MensajeEscrito el 04 Oct 2011 01:24 pm
Lo que no encuentra parece estar en la funcion onTextoSet. Comenta todas las líneas de esa función, verifica que no se de el error, luego ve habilitando de a una hasta que detectes cual es la línea que lo genera

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 04 Oct 2011 07:48 pm
Nada, comentando todas las lineas y solo crear el popUp salta el mismo error. :|

Mi pregunta es... ¿cuales son los eventos que se accionan al pulsar sobre un textInput?

El error me sale cuando pulso con el ratón sobre el area de textInput para introducir algun caracter, si pulso sobre los botones aceptar o cancelar va bien :twisted:

Por alvaroqp85

7 de clabLevel



 

firefox
Citar            
MensajeEscrito el 04 Oct 2011 08:05 pm
El error es que quieres acceder a algo que no existe, es decir viene de un código que pusiste. En principio no veo que tengas ningún listener al setFocus del TextField o similar, y los eventos se generan al darle al botón. Entonces lo primero es encontrar donde se está generando el error (que es muy genérico y no ayuda demasiado) Ve quitando código hasta que deje de ocurrir.

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 04 Oct 2011 10:57 pm
gracias por tus consejos Jorge pero sigo teniendo el mismo problema:
TypeError: Error #1009: Cannot access a property or method of a null object reference.

He reducido el codigo a la minima expresion pero no consigo dar con ello, lo posteo a continuación:

Clase ppal:

Código :

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="HomeView">

<fx:Script>
<![CDATA[
import components.TextPicker;
import components.TextPickerEvent;
import mx.managers.PopUpManager;

[Bindable]
public var textoUsuario:String;

public function getTexto():void{
var textPicker: TextPicker = new TextPicker();
textPicker.addEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextoSet);
textPicker.addEventListener(TextPickerEvent.TEXT_PICKER_CANCEL, onTextoCancel);
PopUpManager.addPopUp(textPicker, this, true);
}

private function onTextoSet(event:TextPickerEvent): void {
textoUsuario = event.textValue as String;
trace("Valor introduci (vista Home) = " + textoUsuario);
onTextoCancel(event);
}

private function onTextoCancel(event:TextPickerEvent): void {
var textoItemVar:TextPicker = event.target as TextPicker;
textoItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextoSet);
textoItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_CANCEL, onTextoCancel);
}
]]>
</fx:Script>

<s:Button id="btLogin" label="popUp" click="getTexto()"/>
</s:View>

TextPicker.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" width="444" height="236">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;

[Bindable]
public var textValue:String;

private function setText():void {
var textoUsuario:TextPickerEvent = new TextPickerEvent(TextPickerEvent.TEXT_PICKER_SET);
dispatchEvent(textoUsuario);
PopUpManager.removePopUp(this);
trace("Valor introducido: " + userText.text);
}
private function cancelText():void {
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>

<s:TextInput id="userText" x="1" y="71" width="100%"/>
<s:Button x="118" y="167" width="200" label="Aplicar" click="setText()" fontSize="20"/>

</s:Group>

TextPicker.as
package components {

import flash.events.Event;

public class TextPickerEvent extends Event {
public static const TEXT_PICKER_SET: String = 'textPickerSet';
public static const TEXT_PICKER_CANCEL: String = 'textPickerCancel';

public var textValue: String;

public function TextPickerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) {
super(type, bubbles, cancelable);
}
}
} 

PD: Es un proyecto flex mobile

Por alvaroqp85

7 de clabLevel



 

firefox
Citar            
MensajeEscrito el 04 Oct 2011 11:23 pm
Ok, si es Mobile ya tienes un problema: no uses PopUpManager, en vez de eso se usa un SkinnablePopUpContainer con un TitleWindow por ejemplo, mira el Tour de Flex http://www.adobe.com/devnet/flex/tourdeflex.html, apartado Mobile, ejemplo de Alert (es lo mismo para un pop-up) Es muy importante seguir los consejos de Adobe en el tema de rendimiento, no basta solo con usar una aplicación basadas en vistas, los recursos son críticos. Si tienes un dispositivo a mano, siempre haz pruebas parciales, en tu PC o Mac todo funciona muy bien, pero a veces en los dispositivos el rendimiento no es aceptable.

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 05 Oct 2011 11:20 am
Cierto, por eso tardaba tanto en cargar las vistas! gracias
Pero el problema le tengo al no poder recibir el string introducido por el usuario en la vista principal, de hecho no entra en 'onTextSet' al pulsar sobre el boton set.

vista ppal:

Código ActionScript :

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark" 
      title="Introducir Texto por el usuario" 
      viewActivate="view1_viewActivateHandler(event)">
   
   <fx:Script>
      <![CDATA[
         import components.MyPopupComponent;
         import components.TextPickerEvent;
         import spark.components.SkinnablePopUpContainer;
         import spark.events.ViewNavigatorEvent;
         
         protected var popup:MyPopupComponent;
         protected var textoUsuario:String;
         
         protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
         {
            popup = new MyPopupComponent();
         }
         
         public function getText():void{
            
            //crear el popup
            var textVar: MyPopupComponent = new MyPopupComponent();
            textVar.addEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextSet);
            textVar.addEventListener(TextPickerEvent.TEXT_PICKER_CANCEL, onTextCancel);
            popup.open(this);
         }
         
         private function onTextSet(event:TextPickerEvent): void
         {   
            textoUsuario = event.textValue;
            trace ("VISTA GENERAL. Valor: " + textoUsuario);
            onTextCancel(event);
         }
         
         private function onTextCancel(event:TextPickerEvent): void
         {
            var comboItemVar:MyPopupComponent = event.target as MyPopupComponent;
            comboItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_SET, onTextSet);
            comboItemVar.removeEventListener(TextPickerEvent.TEXT_PICKER_CANCEL, onTextCancel);
         }
      ]]>
   </fx:Script>
   
   <s:layout>
      <s:VerticalLayout paddingTop="5" paddingBottom="5" paddingLeft="5" paddingRight="5" gap="10" 
                    horizontalAlign="center" verticalAlign="top"/>
   </s:layout>
   
   <s:TextArea width="98%" text="Skinnable pop-up."/>
   
   <s:HGroup>
      <s:Button label="Open Popup" click="getText()"/>
   </s:HGroup>
</s:View>


MyPopUpComponent.mxml

Código ActionScript :

<?xml version="1.0" encoding="utf-8"?>
<s:SkinnablePopUpContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     height="94">
   
   <fx:Script>
      <![CDATA[
         public var textUser:String;
         
         private function setText():void {
            var dpe1:TextPickerEvent = new TextPickerEvent(TextPickerEvent.TEXT_PICKER_SET);
            dpe1.textValue = userTextId.text;
            dispatchEvent(dpe1);
            
            textUser = userTextId.text;
            this.close();
            trace("Valor introducido: " + textUser);
         }
         
         private function cancelText():void {
            this.close();
         }
      ]]>
   </fx:Script>
         <s:Label width="100%" height="57" fontSize="20" text="Introduce tu valor:"/>
         <s:TextInput id="userTextId" width="100%" enabled="true"/>
         <s:HGroup x="0" y="42" width="100%" height="47">
            <s:Button width="100%" label="Set" click="setText()" fontSize="20"/>
            <s:Button width="100%" label="Cancel" click="cancelText()" fontSize="20"/>
         </s:HGroup>
</s:SkinnablePopUpContainer>


TextPickerEvent.as

Código ActionScript :

package components
{
   import flash.events.Event;
   
   public class TextPickerEvent extends Event
   {
      public static const TEXT_PICKER_SET: String = 'textPickerSet';
      public static const TEXT_PICKER_CANCEL: String = 'textPickerCancel';
      
      public var textValue: String;
      
      public function TextPickerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
      {
         super(type, bubbles, cancelable);
      }
   }
}

Por alvaroqp85

7 de clabLevel



 

firefox
Citar            
MensajeEscrito el 05 Oct 2011 11:51 am
Me respondo yo mismo para que le sirva a la gente que tiene el mismo problema.
La clave esta en la sentencia:
close(true, userTextId.text);

En este caso retornamos a la funcion ppal el campo userTextId.text

¡¡¡ Por fin !!!!

Por alvaroqp85

7 de clabLevel



 

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.