Android的 - 传递对象到另一个活动对象、Android

2023-09-03 23:05:48 作者:笑脸带眼红

我利用后续类,这是我作为一个对象: http://pastebin.com/rKmtbDgF

和我想整个使用它传递:

 意图书目=新的意向书(getBaseContext(),BookList.class);

束束=新包();
bundle.putParcelable(imageManager,(Parcelable)IM);
booklist.putExtras(包);
booklist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(书目);
 

和我想用接受它:

  ImageManager IM =(ImageManager)getIntent()getExtras()getParcelable(imageManager)。
 
安卓系统和iOS系统到底有哪些不一样 各有什么优缺点 来看看

我收到以下错误:

  java.lang.ClassCastException:com.example.example.ImageManager
 

解决方案

要做到这一点最简单的方法是实施可序列化。

 的Bean;

@燮pressWarnings(串行)
公共类学生实现Serializable {

公益助学(INT年龄,字符串名称){
    this.age =年龄;
    this.name =名称;
}

公众诠释getAge(){
    返回年龄;
}
公共无效setAge(INT岁){
    this.age =年龄;
}
公共字符串的getName(){
    返回this.name;
}
公共无效setname可以(字符串名称){
    this.name =名称;
}

私人诠释年龄;
私人字符串名称;

}
 

发送的活动一个对象b活动

 学生学生=新的学生(18,扎尔êAhmer);
意图I =新的意图(这一点,B.class);
i.putExtra(studentObject,学生);
startActivity(ⅰ);
 

获取对象在b活动。

 意图I = getIntent();
学生学生=(学生)i.getSerializableExtra(studentObject);
 

I am utilizing the follow class, which I have as an object: http://pastebin.com/rKmtbDgF

And I am trying to pass it across using:

Intent booklist = new Intent(getBaseContext(), BookList.class);

Bundle bundle = new Bundle();
bundle.putParcelable("imageManager", (Parcelable) im);                
booklist.putExtras(bundle);
booklist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(booklist);

And I am trying to receive it using:

ImageManager im  = (ImageManager) getIntent().getExtras().getParcelable("imageManager");

I am getting the following error:

java.lang.ClassCastException: com.example.example.ImageManager

解决方案

Easiest Way to do this is to implement Serializeable ..

import java.io.Serializable;

@SuppressWarnings("serial") 
public class Student implements Serializable {

public Student(int age, String name){
    this.age = age;
    this.name = name;
}

public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

private int age;
private String name;

}

Sending object from Activity A to Activity B

Student student = new Student (18,"Zar E Ahmer");
Intent i = new Intent(this, B.class);
i.putExtra("studentObject", student);
startActivity(i);

Getting Object in Activity B.

Intent i = getIntent();
Student student = (Student)i.getSerializableExtra("studentObject");