【问题标题】:How can I send an object from one Activity to another with Intent.putExtra and Parcel?如何使用 Intent.putExtra 和 Parcel 将对象从一个 Activity 发送到另一个?
【发布时间】:2011-02-20 10:18:53
【问题描述】:

使用堆栈溢出问题How to send an object from one Android Activity to another using Intents?,我做了一个示例应用程序如下。

文件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());
    }
}

但它是我得到空的结果类。怎么了?

【问题讨论】:

    标签: android android-intent parcelable


    【解决方案1】:

    在:

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

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

    【讨论】:

      【解决方案2】:

      我觉得你应该改成这样:

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

      【讨论】:

        【解决方案3】:

        根据getParcelableExtra 的文档,它声明如果找不到可打包的内容,则返回 null。

        在你的 Result.java 文件中试试这个:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-01
          • 1970-01-01
          • 2013-08-14
          相关资源
          最近更新 更多