Lectura y escritura de matriz de números enteros a paquete
No pude encontrar ninguna solución para la forma de tratar con matriz de entero en caso de paquete (Quiero utilizar estas dos funciones dest.writeIntArray (storeId), y in.readIntArray (storeId); ).
Aquí está mi código
- Persistencia de un objeto Parcelable en Android
- Android | Unmarshalling Tipo desconocido al utilizar datos parcelables en paquetes
- ArrayList no se puede convertir en Parcelable
- Poner un objeto en el mensaje de Handler
- Beneficio de usar Parcelable en lugar de serializar objetos
public class ResponseWholeAppData implements Parcelable { private int storeId[]; public int[] getStoreId() { return storeId; } public void setStoreId(int[] storeId) { this.storeId = storeId; } @Override public int describeContents() { return 0; } public ResponseWholeAppData(){ storeId = new int[2]; storeId[0] = 5; storeId[1] = 10; } public ResponseWholeAppData(Parcel in) { if(in.readByte() == (byte)1) in.readIntArray(storeId); //how to do this storeId=in.readIntArray(); ? } } @Override public void writeToParcel(Parcel dest, int flags) { if(storeId!=null&&storeId.length>0) { dest.writeByte((byte)1); dest.writeIntArray(storeId); } else dest.writeByte((byte)0); } public static Parcelable.Creator<ResponseWholeAppData> getCreator() { return CREATOR; } public static void setCreator(Parcelable.Creator<ResponseWholeAppData> creator) { CREATOR = creator; } public static Parcelable.Creator<ResponseWholeAppData> CREATOR = new Parcelable.Creator<ResponseWholeAppData>() { public ResponseWholeAppData createFromParcel(Parcel in) { return new ResponseWholeAppData(in); } public ResponseWholeAppData[] newArray(int size) { return new ResponseWholeAppData[size]; } }; }
- Implementación parcelable con ArrayList <objeto personalizado>
- Cómo implementar correctamente Parcelable con un ArrayList <Parcelable>?
- Problema de desacoplamiento de parcelables
- Cómo utilizar Parcel.readBooleanArray ()?
- Anidado Parcelling: RuntimeException - Unmarshalling tipo desconocido código 3211319 en el desplazamiento 440
- ¿Cómo almacenar permanentemente objeto personalizado parcelable?
- Cómo enviar objetos a través de un paquete
- Pase de ArrayList <? implementa Parcelable> a Actividad
Cuando utilizo " in.readIntArray(storeID)
", obtengo un error:
"Causado por: java.lang.NullPointerException en android.os.Parcel.readIntArray (Parcel.java:672)" .
En lugar de usar " readIntArray
" usé lo siguiente:
storeID = in.createIntArray();
Ahora no hay errores.
Supongo que la clase MyObj implementa Parcelable e implementa todos los métodos requeridos; Voy a sugerir aquí sólo los detalles sobre la lectura / escritura de paquetes.
Si se conoce el tamaño de la matriz de antemano:
public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeIntArray(mMyIntArray); // In this example array length is 4 } protected MyObj(Parcel in) { super(in); mMyIntArray = new int[4]; in.readIntArray(mMyIntArray); }
De otra manera:
public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(mMyArray.length); // First write array length out.writeIntArray(mMyIntArray); // Then array content } protected MyObj(Parcel in) { super(in); mMyIntArray = new int[in.readInt()]; in.readIntArray(mMyIntArray); }
- Cómo hacer whatsapp tipo de animación para abrir el menú de la barra de herramientas (barra de acción)
- Calling finish () Después de iniciar una nueva actividad