Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何使用Intent.putExtra和Parcel将对象从一个Activity发送到另一个Activity?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用以下URL
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());
    }
}

但是Result类I变为null.怎么了?

解决方法

在:

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

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

大佬总结

以上是大佬教程为你收集整理的android – 如何使用Intent.putExtra和Parcel将对象从一个Activity发送到另一个Activity?全部内容,希望文章能够帮你解决android – 如何使用Intent.putExtra和Parcel将对象从一个Activity发送到另一个Activity?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。