Android Intent在不同应用程序之间传递对象

huangapple go评论66阅读模式
英文:

Android Intent pass Object between different different apps

问题

我有这个 Activity(MainActivity,来自包名为 com.example.myapplication 的包),它接收一个名为“BeanValidacion”的类的 Intent

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

    Bundle willirex = getIntent().getExtras();

    if(willirex!=null)
    {
        BeanValidacion datos = (BeanValidacion) willirex.get("paramIn");

        Toast.makeText(MainActivity.this,datos.getIdNumber(),Toast.LENGTH_LONG).show();
    }
}

而我想通过另一个 APK(包名为 com.adon.intents 的包)来调用它,并发送一个 Intent,就像我用字符串 Intent 做的那样,但这一次,我想发送一个名为“BeanValidacion”的类的对象。

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

    BeanValidacion datos = new BeanValidacion();
    datos.setIdNumber("12345");
    Intent next = new Intent();
    next.setClassName("com.example.myapplication", "com.example.myapplication.MainActivity");
    next.putExtra("paramIn", datos);

    startActivity(next);
}

当我执行 com.adon.intents APK 来发送这个 Intent 时,在 com.example.myapplication 中出现了这个错误:

> ClassNotFoundException 读取可序列化对象(名称 = com.adon.intents.BeanValidacion)

出现在这行代码:

Toast.makeText(MainActivity.this, datos.getIdNumber(), Toast.LENGTH_LONG).show();

在仅发送一个字符串时不会发生这种情况,但我在解决这个问题时遇到了问题。

谢谢。

英文:

I have this Activity (the MainActivity, from package package com.example.myapplication) that receives an Intent of class "BeanValidacion".

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

        Bundle willirex = getIntent().getExtras();

        if(willirex!=null)
        {
            BeanValidacion datos = (BeanValidacion) willirex.get("paramIn");

            Toast.makeText(MainActivity.this,datos.getIdNumber(),Toast.LENGTH_LONG).show();


        }

And I want to call it with a different APK (package com.adon.intents) and send an Intent, as I do with String Intents, but this time, I want to send an object of class "BeanValidacion":


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

        BeanValidacion datos = new BeanValidacion();
        datos.setIdNumber("12345");
        Intent next = new Intent();
        next.setClassName("com.example.myapplication", "com.example.myapplication.MainActivity");
        next.putExtra("paramIn", datos);

        startActivity(next);

    }

When I execute the com.adon.intents APK to send the intent, I got this error on com.example.myapplication:

> ClassNotFoundException reading a Serializable object (name = com.adon.intents.BeanValidacion

In this line:


Toast.makeText(MainActivity.this,datos.getIdNumber(),Toast.LENGTH_LONG).show();

This doesn't happen when sending just a String, but I am having problems solving this one.

Regards

答案1

得分: 1

你应该将 BeanValidacion.java 文件放在第二个应用程序中与 com.adon.intents.BeanValidacion 完全相同的包层次结构中。此外,这两个类的 serialVersionUID 应该相同。

英文:

You should put the BeanValidacion.java file in the exact package hierarchy com.adon.intents.BeanValidacion on the second app. Also, both classes should have the same serialVersionUID.

huangapple
  • 本文由 发表于 2020年9月8日 19:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63792789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定