如何使用 Intent.putExtra 和 Parcel 将对象从一个 Activity 发送到另一个 Activity?
使用 Stack Overflow 问题如何使用 Intents 将对象从一个 Android Activity 发送到另一个 Android Activity?,我制作了一个示例应用程序,如下所示。
文件 Scrollview1.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Scrollview1 extends Activity {
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Scrollview1.this, Result.class);
Name n = new Name();
n.setI(20);
n.setS1("Hello");
n.setS2("World");
i.putExtra("Name", n);
startActivity(i);
}
});
}
}
文件 Name.java
import android.os.Parcel;
import android.os.Parcelable;
public class Name implements Parcelable {
int i;
String s1;
String s2;
public Name() {
}
private Name(Parcel in) {
in.readInt();
in.readString();
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getS1() {
return s1;
}
public void setS1(String s) {
this.s1 = s;
}
public String getS2() {
return s2;
}
public void setS2(String s) {
this.s2 = s;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(i);
dest.writeString(s1);
dest.writeString(s2);
}
public static final Name.Creator<Name> CREATOR = new Name.Creator<Name>() {
public Name createFromParcel(Parcel in) {
return new Name(in);
}
public Name[] newArray(int size) {
return new Name[size];
}
};
}
文件 Result.java
import android.app.Activity;
import android.os.Bundle;
public class Result extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Name a = (Name)getIntent().getParcelableExtra("Name");
System.out.println("Int: " + a.getI());
System.out.println("String: " + a.getS1());
System.out.println("String: " + a.getS2());
}
}
但它是 Result 类,我得到 null。怎么了?
Using Stack Overflow question How to send an object from one Android Activity to another using Intents?, I have made a sample application as follows.
File Scrollview1.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Scrollview1 extends Activity {
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Scrollview1.this, Result.class);
Name n = new Name();
n.setI(20);
n.setS1("Hello");
n.setS2("World");
i.putExtra("Name", n);
startActivity(i);
}
});
}
}
File Name.java
import android.os.Parcel;
import android.os.Parcelable;
public class Name implements Parcelable {
int i;
String s1;
String s2;
public Name() {
}
private Name(Parcel in) {
in.readInt();
in.readString();
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getS1() {
return s1;
}
public void setS1(String s) {
this.s1 = s;
}
public String getS2() {
return s2;
}
public void setS2(String s) {
this.s2 = s;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(i);
dest.writeString(s1);
dest.writeString(s2);
}
public static final Name.Creator<Name> CREATOR = new Name.Creator<Name>() {
public Name createFromParcel(Parcel in) {
return new Name(in);
}
public Name[] newArray(int size) {
return new Name[size];
}
};
}
File Result.java
import android.app.Activity;
import android.os.Bundle;
public class Result extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Name a = (Name)getIntent().getParcelableExtra("Name");
System.out.println("Int: " + a.getI());
System.out.println("String: " + a.getS1());
System.out.println("String: " + a.getS2());
}
}
But it is Result class I getting null. What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
In:
您正在从 Parcel 中读取信息,但实际上并未将信息分配给变量。
In:
You are reading from the Parcel, but not actually assigning the information to a variable.
我认为你应该把它改成这样:
I think you should change it to this:
根据
getParcelableExtra
的文档,它指出如果未找到可打包内容,则返回 null。在您的 Result.java 文件中尝试一下:
而不是您现在正在做的事情,
As per the documentation for the
getParcelableExtra
, it states that it returns null if the parcelable content is not found.Try this in your Result.java file:
Instead of what you are doing now,