如何使用 Intent.putExtra 和 Parcel 将对象从一个 Activity 发送到另一个 Activity?

发布于 2024-10-18 04:07:02 字数 2931 浏览 6 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-10-25 04:07:02

In:

private Name(Parcel in){
    in.readInt();
    in.readString();
}

您正在从 Parcel 中读取信息,但实际上并未将信息分配给变量。

In:

private Name(Parcel in){
    in.readInt();
    in.readString();
}

You are reading from the Parcel, but not actually assigning the information to a variable.

就此别过 2024-10-25 04:07:02

我认为你应该把它改成这样:

private Name(Parcel in){
    i  = in.readInt();
    s1 = in.readString();
    s2 = in.readString();
}

I think you should change it to this:

private Name(Parcel in){
    i  = in.readInt();
    s1 = in.readString();
    s2 = in.readString();
}
肤浅与狂妄 2024-10-25 04:07:02

根据 getParcelableExtra 的文档,它指出如果未找到可打包内容,则返回 null。

在您的 Result.java 文件中尝试一下:

Name a = (Name)getIntent().getExtras().getParcelable("Name");

而不是您现在正在做的事情,

Name a = (Name)getIntent().getParcelableExtra("Name");

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:

Name a = (Name)getIntent().getExtras().getParcelable("Name");

Instead of what you are doing now,

Name a = (Name)getIntent().getParcelableExtra("Name");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文