标签: parcel

parcelable在哪里/何时使用describeContents()?

有谁知道调用Parcelable的这种方法的位置/时间?

@Override
public int describeContents() {
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它必须被覆盖.但是,我应该考虑做一些有用的事情吗?

android parcel parcelable

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

如何在Android中使用Parcel?

我正在尝试用来Parcel写,然后回读一个Parcelable.出于某种原因,当我从文件中读回对象时,它又回来了null.

public void testFoo() {
    final Foo orig = new Foo("blah blah");

    // Wrote orig to a parcel and then byte array
    final Parcel p1 = Parcel.obtain();
    p1.writeValue(orig);
    final byte[] bytes = p1.marshall();


    // Check to make sure that the byte array seems to contain a Parcelable
    assertEquals(4, bytes[0]); // Parcel.VAL_PARCELABLE


    // Unmarshall a Foo from that byte array
    final Parcel p2 = Parcel.obtain();
    p2.unmarshall(bytes, 0, bytes.length);
    final Foo result = (Foo) p2.readValue(Foo.class.getClassLoader());


    assertNotNull(result); …
Run Code Online (Sandbox Code Playgroud)

android parcel

55
推荐指数
4
解决办法
8万
查看次数

Android E/Parcel:解组时找不到类(仅限三星Tab3)

我一直无法解决为什么会出现这种错误,并且只能在运行4.4.2的三星Tab3设备上解决?它发生在我的MainActivity启动另一个Activity时,并在intent中传递一个Parcelable类,如下所示:

    private void debugTest(TestParcel cfgOptions){
        TestParcel cfgOptions = new TestParcel();
        cfgOptions.setValue(15); //just to verify

        Intent intent = new Intent(MainActivity.this, TestActivity.class);
        intent.putExtra("cfgOptions", cfgOptions);
        startActivityForResult(intent, DBG_TEST);
    }
Run Code Online (Sandbox Code Playgroud)

TestActivity获取可分配的数据,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_activity);

    TestParcel cfgOptions = getIntent().getExtras().getParcelable("cfgOptions");
}
Run Code Online (Sandbox Code Playgroud)

TestParcel类:

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

    public class TestParcel implements Parcelable {
    private long l_ucs_value = 0;
    private String s_rx_number = "";

    //constructor
    public TestParcel() {
        l_ucs_value = 0;
        s_rx_number = "";
    }

    public void RxNumber(String s) {
        s_rx_number = s;
    }
    public …
Run Code Online (Sandbox Code Playgroud)

android unmarshalling parcel android-intent classnotfoundexception

50
推荐指数
4
解决办法
4万
查看次数

"使用包含ClassLoader作为参数的Parcel.read方法时,解组<myclass>"时出现"BadParcelableException:ClassNotFoundException"

给定一个自定义类org.example.app.MyClass implements Parcelable,我想写List<MyClass>一个包.我做了编组

 List<MyClass> myclassList = ...
 parcel.writeList(myclassList);
Run Code Online (Sandbox Code Playgroud)

每当我试图解散课程时

 List<MyClass> myclassList = new ArrayList<MyClass>();
 parcel.readList(myclassList, null);
Run Code Online (Sandbox Code Playgroud)

有一个"BadParcelableException: ClassNotFoundException when unmarshalling org.example.app.MyClass"例外.

这有什么不对?为什么找不到课程?

android marshalling unmarshalling parcel

49
推荐指数
4
解决办法
3万
查看次数

如何在Parcel的帮助下将Parcelable编组并解组为字节数组?

我想编组和解组一个实现Parcelable/来自字节数组的类.我很清楚Parcelable表示不稳定,因此不适用于实例的长期存储.但我有一个用例,我需要序列化一个对象,如果由于内部Android更改而解组失败,它不是一个showstopper.该类已经在实现该Parcelable接口.

给定一个类MyClass implements Parcelable,我如何(ab)使用Parcelable接口进行编组/解组?

android marshalling unmarshalling parcel parcelable

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

如何在Android上读取和写入Enum到包裹?

这是我的模型类:

public enum Action {
    RETRY, SETTINGS
}

private int imageId;
private String description;
private String actionName;
private Action action;

public NetworkError(int imageId, String description, String actionName, Action action ) {
    this.imageId = imageId;
    this.description = description;
    this.actionName = actionName;
    this.action = action;
}

public int getImageId() {
    return imageId;
}

public void setImageId(int imageId) {
    this.imageId = imageId;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getActionName() { …
Run Code Online (Sandbox Code Playgroud)

enums android parcel parcelable

41
推荐指数
5
解决办法
2万
查看次数

包裹 SemVer 错误

所以,我以前多次使用 Parcel,我从来没有遇到过问题。这一次它抛出了一些关于 SemVer 版本控制的愚蠢错误,我真的很想找到解决这个问题的解决方案。

我开始了新项目:安装了 npm w/ npm init(没有其他选项),然后安装了 Parcel npm install --save-dev parcel-bundler,然后创建了我的文件夹结构:

--node_modules
--index.html
--index.js

这是我的 package.json:

{
  "name": "playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "parcel index.html"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "parcel-bundler": "^1.12.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经配置了默认的 npm 脚本来运行包:"dev": "parcel index.html"并运行它。一切正常,但是当我通过<script src="/index.js"></script>它在 index.html 中连接我的 index.js 时,它会抛出很大的错误,说:

D:\workingSpace\playground\index.js: Invalid Version: undefined
  at new SemVer (D:\workingSpace\playground\node_modules\@babel\preset-env\node_modules\semver\semver.js:314:11)
  at compare (D:\workingSpace\playground\node_modules\@babel\preset-env\node_modules\semver\semver.js:647:10)   
  at lt (D:\workingSpace\playground\node_modules\@babel\preset-env\node_modules\semver\semver.js:688:10)        
  at D:\workingSpace\playground\node_modules\@babel\preset-env\lib\index.js:276:22
  at Object.default (D:\workingSpace\playground\node_modules\@babel\helper-plugin-utils\lib\index.js:22:12)     
  at …
Run Code Online (Sandbox Code Playgroud)

parcel node.js semantic-versioning node-modules

40
推荐指数
2
解决办法
8035
查看次数

Android Parcelable和Serializable

所以我知道建议在Android中使用Parcelable而不是Serializable,因为它更快.

我的问题是:使用Serializable是不可避免的吗?

如果我有一个我想要序列化的自定义对象,那么假设我有以下类定义

public class Person {
   String name;
   int Age;
   ...
   ....
}
Run Code Online (Sandbox Code Playgroud)

使这个parcelable很容易,因为Person类包含parcel.write*()支持的类型,即parcel.writeString和parcel.writeInt

现在,如果Person类如下:

public class PersonTwo {
   MyCustomObj customObj;
   String name;
   int Age;
   ...
   ....
}
Run Code Online (Sandbox Code Playgroud)

我怎么想包裹MyCustomObj对象?看来我需要再次使用serializable吗?但同样,我认为使用serializable是慢的,似乎我们别无选择,只能在这种情况下使用它.

我不明白

谁能告诉我在这种情况下我会如何包裹PersonTwo?

serialization android parcel parcelable android-intent

30
推荐指数
2
解决办法
3万
查看次数

错误:由于版本无效或不受支持,无法反序列化克隆数据

我使用 Parcel 来部署我的 Web 项目,有一次我在尝试部署页面时遇到了以下错误。我尝试了在网上看到的几种解决方案,但没有一个对我有用,我什至不明白一些。

  Error: Unable to deserialize cloned data due to invalid or unsupported version.
      at deserialize (node:v8:345:7)
      at deserialize (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\serializer.js:249:48)
      at RequestTracker.getRequestResult (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\RequestTracker.js:635:54)
      at async RequestTracker.runRequest (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\RequestTracker.js:725:20)
      at async applyRuntimes (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\applyRuntimes.js:174:7)
      at async BundlerRunner.bundle (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\requests\BundleGraphRequest.js:287:25)
      at async RequestTracker.runRequest (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce Site\node_modules\@parcel\core\lib\RequestTracker.js:725:20)
      at async Object.run (C:\Users\David Etuk\Documents\Front End Mentor Projects\E-Commerce …
Run Code Online (Sandbox Code Playgroud)

parcel npm

29
推荐指数
2
解决办法
3万
查看次数

如何解决 Eslint - Module.createRequire is not a function 错误?

我正在将 React 应用程序与 Parcel 捆绑器和 TypeScript 结合使用。我正在尝试使用 ESLint 但出现此错误:

ESLint:初始化错误(ESLint)。Module.createRequire 不是函数。

版本:

  • IDE:网络风暴
  • 节点版本:16.6.2
  • NPM版本:7.20.3
  • 我的机器上没有全局安装 ESLint

这是我的设置:

包.json:

{
  "name": "app-boilerplate",
  "version": "1.0.0",
  "description": "",
  "source": "public/index.html",
  "scripts": {
    "start": "parcel --port=3000 --open --lazy",
    "build": "parcel build",
    "lint": "eslint . --ext .tsx,.ts",
    "lint:fix": "eslint . --ext .tsx,.ts --fix",
    "test": "jest --coverage --passWithNoTests",
    "test:watch": "jest --watchAll --coverage",
    "prepare": "husky install",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.16.7",
    "@parcel/transformer-svg-react": "^2.2.0",
    "@storybook/addon-actions": "^6.4.13",
    "@storybook/addon-essentials": …
Run Code Online (Sandbox Code Playgroud)

javascript parcel typescript reactjs eslint

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