标签: parcelable

如何:在Android中包裹位图

我有一个序列化的类,我想添加一个位图,但Bitmap不支持序列化.

相反,我认为我会使用包裹,但无法让它工作.

这是使用局部变量的一些测试代码:

    Parcel parcel;
    Bitmap sourceBitmap;
    Bitmap destinationBitmap;
    parcel = Parcel.obtain();

    sourceBitmap = Bitmap.createBitmap(200, 400, Config.ARGB_8888);

    sourceBitmap.writeToParcel(parcel, 0);

    destinationBitmap = Bitmap.CREATOR.createFromParcel(parcel);
Run Code Online (Sandbox Code Playgroud)

我在上面的最后一行收到以下错误:

09-06 21:18:20.463: DEBUG/skia(17716): Bitmap_createFromParcel unknown config: 0
09-06 21:18:20.473: DEBUG/AndroidRuntime(17716): Shutting down VM
09-06 21:18:20.483: WARN/dalvikvm(17716): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-06 21:18:20.493: ERROR/AndroidRuntime(17716): Uncaught handler: thread main exiting due to uncaught exception
09-06 21:18:20.513: ERROR/AndroidRuntime(17716): java.lang.RuntimeException: Failed to unparcel Bitmap
09-06 21:18:20.513: ERROR/AndroidRuntime(17716):     at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:899)
09-06 21:18:20.513: ERROR/AndroidRuntime(17716):     at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:903)
Run Code Online (Sandbox Code Playgroud)

android bitmap parcel parcelable

13
推荐指数
2
解决办法
1万
查看次数

Android在SQLite中存储一个Parcelable对象

我需要在Sqlite中存储对象内容.我想知道哪个是使用对象序列化或使用Parcelable进行此操作的最佳方法.

是否可以将其存储为Parcelable?我该怎么做?

sqlite android parcelable

12
推荐指数
1
解决办法
1万
查看次数

如何从Parcel中检索使用TextUtils.writeToParcel(...)保存的CharSequence?

我希望能够将我的应用程序活动之一的格式化文本(即格式跨度的文本)发送到另一个.这些CharSequence实例深入Parcelable到我创建的某些类型中.

例如,当编组其中一个带有格式化CharSequences 的类型时,我使用TextUtils.writeToParcel如下:

public class HoldsCharSequence {

    /* some formatted string that uses basic spans (e.g., ForegroundColorSpan) */
    private CharSequence cs;

    public void writeToParcel(Parcel out, int flags) {
        TextUtils.writeToParcel(cs, out, 0);
    }

    /* ... rest of the code ... */
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何CharSequenceParcel我的私有构造函数中检索.

以下就不能正常工作:

private HoldsCharSequence(Parcel in) {
    cs = (CharSequence) in.readValue(CharSequence.class.getClassLoader());
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling
Run Code Online (Sandbox Code Playgroud)

还有两件事:1.我已经成功实现了自己的自定义Parcelable对象,CharSequence特别是问题.2.我知道TextUtils.writeToParcel在保存文本格式方面会做出最大努力,我可以忍受.

android parcelable

12
推荐指数
1
解决办法
2567
查看次数

我可以将自定义对象发送到Android Wear吗?

我刚刚学习如何开发Android Wear,我已经为智能手表创建了一个全屏活动,在我的应用程序的移动部分,我获得了一些JSON数据,并从中创建了一个自定义对象列表.

在我的移动应用程序中,我在ListView中显示这些对象的信息.

在我的应用程序的Wear部分,我想显示此列表的限制版本,例如列表中的前3个将显示在Wearable上的全屏应用程序上.

我的问题是,似乎没有办法将Parcelable Objects发送到Android Wear,因此在DataItem中没有putParcelable选项.

看起来唯一的选择是以字节为单位发送对象,如下所示:

public void sendWearableData(GoogleApiClient aGoogleApiClient, ArrayList<MyObject> myObjectList, String path)
{

    googleApiClient = aGoogleApiClient;

    byte[] testBytes = new byte[0];

    if (googleApiClient.isConnected()) {
        PutDataMapRequest dataMapRequest = PutDataMapRequest.create(path);
        try {
            testBytes = BytesUtils.toByteArray(myObjectList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataMapRequest.getDataMap().putByteArray(Constants.TEST_KEY, testBytes);
        PutDataRequest request = dataMapRequest.asPutDataRequest();
        Wearable.DataApi.putDataItem(googleApiClient, request);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我必须将我的对象转换为字节,将其发送到Android Wear并将其转换回来?这意味着我已经实现了Parcelable的对象,所以我现在可以通过Intents发送它们也需要实现Serializable,这是正确的还是有更好的方法呢?

android serializable parcelable android-wear-data-api wear-os

12
推荐指数
1
解决办法
4415
查看次数

Android中Bundle的错误幻数

我使用以下代码将数据从一个活动传递到其他活动:

 @Override
    public void execute(List<Report> reports, Questions question) {
        Intent replyIntent = new Intent(listener, ReplyActivity.class);
        replyIntent.putExtra("id", 0L);
        replyIntent.putExtra("questions", question);
        listener.openReportOk(question);
        listener.startActivity(replyIntent);
    }
Run Code Online (Sandbox Code Playgroud)

监听器是回调的活动参考.

问题是这堂课:

@Table(name = "Questions")
public class Questions extends Entity implements Parcelable {

    public static final Creator<Questions> CREATOR = new Creator<Questions>() {
        public Questions createFromParcel(Parcel source) {
            return new Questions(source);
        }

        public Questions[] newArray(int size) {
            return new Questions[size];
        }
    };

    @TableField(name = "idReport", datatype = DATATYPE_INTEGER)
    private int idReport;
    @TableField(name = "nameReport", datatype = DATATYPE_STRING)
    private String …
Run Code Online (Sandbox Code Playgroud)

java android parcelable

12
推荐指数
2
解决办法
6908
查看次数

如何使界面Parcelable?

如何使Interface对象可以Parcelable?

public interface Options{
    public String getAction();
    public String getType();

}


public class OptionsInfo implements Parcelable {

    private int duration;
    private ArrayList<Options> path;

    public OptionsInfo(int duration) {
        super();
        this.duration = duration;
        this.path = new ArrayList<Options>();
    }

    public ArrayList<Options> getPath() {
        return path;
    }

    public void setPath(ArrayList<Options> path) {
        this.path = path;
    }

     public void addToPath(Options object)
        {
            path.add(object);
        }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    @Override
    public int describeContents() { …
Run Code Online (Sandbox Code Playgroud)

android parcelable

12
推荐指数
1
解决办法
3957
查看次数

通过bundle检索parcelable对象是否始终创建新副本?

我通过在创建片段时添加到一个包中将一个parcelable对象传递给一个片段.在onc实例中,对此parceled对象的修改反映了原始对象的修改,而在另一种情况下则不是.我对这种行为感到有点困惑.直到现在我已经假设通过一个包检索一个包围的对象总是创建一个新对象[不确定它是浅拷贝还是深拷贝].

有人请澄清可怜行为.

android parcelable android-fragments android-bundle

12
推荐指数
1
解决办法
1354
查看次数

Parcelable中的标志有什么用?

我一直在编写Parcelables而Parcel没有关注flags字段,这是方法签名中的一个参数,它工作正常,但我遇到了一个我不能再忽略它们的实现:

public static <K extends Parcelable, V extends Parcelable> void write(Parcel dest,
                                                    Map<K, V> map, int flags) {
        if (map == null) {
            dest.writeInt(-1);
        } else {
            Set<Map.Entry<K, V>> entrySet = map.entrySet();
            dest.writeInt(entrySet.size());
            for (Map.Entry<K, V> entry : entrySet) {
                dest.writeParcelable(entry.getKey(), flags);
                dest.writeParcelable(entry.getValue(), flags);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是一个Map 向/从 Parcelable程序我已经写了,我想知道如果标志应传递,因为它是两个关键还有价值,而写他们还是应该通过0 flags.

我阅读了文档中标志的定义:

PARCELABLE_WRITE_RETURN_VALUE

在API级别1中添加

int PARCELABLE_WRITE_RETURN_VALUE
Run Code Online (Sandbox Code Playgroud)

用于的标志writeToParcel(Parcel, int):正在写入的对象是返回值,它是诸如"Parcelable someFunction()", …

flags android parcel parcelable

12
推荐指数
2
解决办法
4038
查看次数

在 Room 数据库实体上实现 Parcelable 是一种好习惯吗?

我正在学习我的 Android 开发技能,并一直在玩架构组件。因此,我使用Room注释创建了一个实体类以将其持久化到数据库并Parcelable在实体类上实现,部分目的是为了将实体对象放入Intent对象中并在活动/片段之间传递它。我只想知道这是否是一个好习惯。使用此类方法是否有任何缺点,例如数据库泄漏或安全漏洞?只是好奇。

performance android parcelable android-intent android-room

12
推荐指数
1
解决办法
3852
查看次数

如何在片段中使用Parcelable获取数据?

我可以在Activity中使用Parcelable,但我不知道如何在Fragment中使用它.

我有一个FragmentGet在ListFragment ListView中显示所有行的数据库,我想在另一个片段的Lis​​tView的详细信息,单击时,我使用Parcelable到片段之间传递数据.

这是我的Test.java:

import android.os.Parcel;
import android.os.Parcelable;

public class Test implements Parcelable {

    private int studentNumber;
    private String studentName;
    private String studentFamily;

    public int getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(int studentNumber) {
        this.studentNumber = studentNumber;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentFamily() {
        return studentFamily;
    }

    public void setStudentFamily(String studentFamily) {
        this.studentFamily = studentFamily;
    }

    public Test() {
    }

    public Test(Parcel read) {
        studentNumber …
Run Code Online (Sandbox Code Playgroud)

android fragment parcelable

11
推荐指数
1
解决办法
2万
查看次数