Class的传递向量对象到其他活动 - 在 Android 中

发布于 2024-12-07 19:46:44 字数 1141 浏览 0 评论 0 原文

我想问一下,如果我想将这些数据传递给其他类,我做错了什么:

String [] codes = {"code"};
Class<?> [] classes = { TestActivity.class };



    Intent i = new Intent();
    Pack p = new Pack();
    p.eat(classes,codes);
    i.putExtra("com.mbar", p);
    i.setClass(this, CaptureActivity.class);
    startActivity(i);

}
}

稍后在其他活动中,我尝试像这样解包:

Bundle b = getIntent().getExtras();
Pack p = (Pack)b.getParcelable("com.mbar");
if(p!=null){
classes = p.getClasses();
codes = p.getNames();

可 Parcelable 的 Pack 类如下所示:

package com.mbar;

import android.os.Parcel;
import android.os.Parcelable;

public class Pack implements Parcelable {
Class<?>[] classes;
String [] codes;

public void eat(Class<?>[] classes,String [] codes){
    this.classes = classes;
    this.codes = codes;
}
public Class<?>[] getClasses(){
    return this.classes;
}
public String [] getNames(){
    return this.codes;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) { 
}
}

I'd like to ask what I'm doing wrong if I want to pass such data to other class:

String [] codes = {"code"};
Class<?> [] classes = { TestActivity.class };



    Intent i = new Intent();
    Pack p = new Pack();
    p.eat(classes,codes);
    i.putExtra("com.mbar", p);
    i.setClass(this, CaptureActivity.class);
    startActivity(i);

}
}

Later in other activity I try it to unpack like that:

Bundle b = getIntent().getExtras();
Pack p = (Pack)b.getParcelable("com.mbar");
if(p!=null){
classes = p.getClasses();
codes = p.getNames();

The Pack class which is Parcelable looks like:

package com.mbar;

import android.os.Parcel;
import android.os.Parcelable;

public class Pack implements Parcelable {
Class<?>[] classes;
String [] codes;

public void eat(Class<?>[] classes,String [] codes){
    this.classes = classes;
    this.codes = codes;
}
public Class<?>[] getClasses(){
    return this.classes;
}
public String [] getNames(){
    return this.codes;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) { 
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪花飘飘的天空 2024-12-14 19:46:44
String [] codes = {"code"};
Class<?> [] classes = { TestActivity.class };



    Intent i = new Intent();
    Pack p = new Pack(classes,codes);
    i.putExtra("com.mbar", p);
    i.setClass(this, CaptureActivity.class);
    startActivity(i)

Pack p = (Pack)getIntent().getParcelableExtra("com.mbar");
if(p!=null){
classes = p.getClasses();
codes = p.getNames();

public static class Pack implements Parcelable {
    Class<?>[] classes;
    String[] codes;

    public Pack(Class<?>[] classes, String[] codes) {
        this.classes = classes;
        this.codes = codes;
    }

    public Pack(Parcel in) {
        int len = in.readInt();
        String[] classnames = new String[len];
        in.readStringArray(classnames);
        classes = new Class<?>[classnames.length];
        for (int i = 0; i < classnames.length; i++) {
            try {
                classes[i] = Class.forName(classnames[i]);
            } catch (ClassNotFoundException e) {
            }
        }
        len = in.readInt();
        codes = new String[len];
        in.readStringArray(codes);
    }

    public Class<?>[] getClasses() {
        return this.classes;
    }

    public String[] getNames() {
        return this.codes;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Pack> CREATOR = new Parcelable.Creator<Pack>() {
        public Pack createFromParcel(Parcel in) {
            return new Pack(in);
        }

        public Pack[] newArray(int size) {
            return new Pack[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        String[] classnames = new String[classes.length];
        for (int i = 0; i < classes.length; i++) {
            classnames[i] = classes[i].getName();
        }
        dest.writeInt(classnames.length);
        dest.writeStringArray(classnames);
        dest.writeInt(codes.length);
        dest.writeStringArray(codes);
    }
}
String [] codes = {"code"};
Class<?> [] classes = { TestActivity.class };



    Intent i = new Intent();
    Pack p = new Pack(classes,codes);
    i.putExtra("com.mbar", p);
    i.setClass(this, CaptureActivity.class);
    startActivity(i)

Pack p = (Pack)getIntent().getParcelableExtra("com.mbar");
if(p!=null){
classes = p.getClasses();
codes = p.getNames();

public static class Pack implements Parcelable {
    Class<?>[] classes;
    String[] codes;

    public Pack(Class<?>[] classes, String[] codes) {
        this.classes = classes;
        this.codes = codes;
    }

    public Pack(Parcel in) {
        int len = in.readInt();
        String[] classnames = new String[len];
        in.readStringArray(classnames);
        classes = new Class<?>[classnames.length];
        for (int i = 0; i < classnames.length; i++) {
            try {
                classes[i] = Class.forName(classnames[i]);
            } catch (ClassNotFoundException e) {
            }
        }
        len = in.readInt();
        codes = new String[len];
        in.readStringArray(codes);
    }

    public Class<?>[] getClasses() {
        return this.classes;
    }

    public String[] getNames() {
        return this.codes;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<Pack> CREATOR = new Parcelable.Creator<Pack>() {
        public Pack createFromParcel(Parcel in) {
            return new Pack(in);
        }

        public Pack[] newArray(int size) {
            return new Pack[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        String[] classnames = new String[classes.length];
        for (int i = 0; i < classes.length; i++) {
            classnames[i] = classes[i].getName();
        }
        dest.writeInt(classnames.length);
        dest.writeStringArray(classnames);
        dest.writeInt(codes.length);
        dest.writeStringArray(codes);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文