英文:
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 Intent
s, 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论