Android:在活动之间传递对象并使其可打包

Android:在活动之间传递对象并使其可打包,android,Android,Android编程新手。无法在活动之间传递对象并使其可包裹。有人能告诉我问题出在哪里吗?我想知道逻辑是否正确 public class MainActivity extends Activity { private Bundle MyActivityParams; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

Android编程新手。无法在活动之间传递对象并使其可包裹。有人能告诉我问题出在哪里吗?我想知道逻辑是否正确

public class MainActivity extends Activity {

    private Bundle MyActivityParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MyActivity.class);
        MyActivityParams = fillInData(MyActivityParams);
        intent.putExtras(MyActivityParams);
        startActivity(intent, savedInstanceState);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    private Bundle fillInData(Bundle bundle){

        bundle.putFloat("com.company.MyActivity.FL", 12);
        bundle.putFloat("com.company.MyActivity.VH", 100);
        bundle.putFloat("com.company.MyActivity.const", 1);
        return bundle;
    }
}

public class DisplayActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayData();

    }
    private void displayData(){
        //ActivityData is from MyActivity
        ActivityData data = new ActivityData(getIntent().getExtras());

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_display, menu);
        return true;
    }
}

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        displayParams();
    }
    private void displayParams(){
        ActivityData dataBundle = new ActivityData((getIntent()).getExtras());
        transfer(dataBundle);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_algorithm, menu);
        return true;
    }

    public void transfer(ActivityData incomingdata){
        startActivity(incomingdata.getIntent());
    }

    protected static class ActivityData{
        private Bundle data;
        private TextView text;
        private Intent intent;
        public ActivityData(Bundle bundle){
            /*data = bundle;
            intent = new Intent(null, DisplayActivity.class);
            text = new TextView(null);*/
        }

        public void display(String key){
            this.text.setText(data.getString(key));
                    //not allowed: startActivity(intent);
        //not allowed either: setContentView(text);
        }

        public Intent getIntent() {
            return intent;
        }
        public void setIntent(Intent intent) {
            this.intent = intent;
        }
        public TextView getText() {
            return text;
        }
        public void setText(TextView text) {
            this.text = text;
        }
        public Bundle getData() {
            return data;
        }
        public void setData(Bundle data) {
            this.data = data;
        }
    }
}

我们来上个人课吧

public class Person {
    private String name;
    private String email;
    private int age;

    public Person(int age, String name, String email) {
        this.age = age;
        this.name = name;
        this.email = email;
    }
}
一个可包裹的看起来像这样

public class ParcelablePerson implements Parcelable {

    private final Person person;

    private ParcelablePerson(Parcel parcel) {
        this.person = new Person(parcel.readInt(), parcel.readString(), parcel.readString());
    }

    public ParcelablePerson(Person person) {
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }

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

    // This is the method where you disassembly your object to pieces
    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeInt(person.getAge());
        parcel.writeString(person.getName());
        parcel.writeString(person.getEmail());
    }

    public static final Creator<ParcelablePerson> CREATOR = new Creator<ParcelablePerson>() {

        // And here you create a new instance from a parcel using the first constructor
        @Override
        public ParcelablePerson createFromParcel(Parcel parcel) {
            return new ParcelablePerson(parcel);
        }

        @Override
        public ParcelablePerson[] newArray(int size) {
            return new ParcelablePerson[size];
        }

    };
}
公共类ParcelablePerson实现Parcelable{
私人最终人;
私人包裹员(包裹){
this.person=新人(parcel.readInt()、parcel.readString()、parcel.readString());
}
公共包裹员(人){
这个人=人;
}
公众人物{
返回人;
}
@凌驾
公共int描述内容(){
返回0;
}
//这是将对象分解为碎片的方法
@凌驾
公共无效writeToParcel(Parcel-Parcel,int标志){
parcel.writeInt(person.getAge());
parcel.writeString(person.getName());
parcel.writeString(person.getEmail());
}
公共静态最终创建者=新创建者(){
//这里使用第一个构造函数从地块创建一个新实例
@凌驾
公共地块人员createFromParcel(地块){
归还新的包裹者(包裹);
}
@凌驾
公共ParcelablePerson[]新数组(整数大小){
返回新的ParcelablePerson[大小];
}
};
}

该过程中的关键点是在
private ParcelablePerson(Parcel-Parcel)
构造函数和
public void writeToParcel(Parcel-Parcel,int-flags)
方法中具有相同的变量顺序。。。你可以在age属性上看到它,它是一个int。

试试这个,这个代码对我有用。但我实现了可序列化而不是可打包。 尝试实现可打包而不是可序列化。如果有用的话,试试看。我没有试过这个,但我可以随身携带

要传递和对象到另一个活动,请执行以下操作:

in activity A
make sure the class implements the serializable interface
create a new intent
create a new bundle 
use the putSerializeable method to add the object to the bundle with a key
put the bundle in the intent by using the putExtras method
start the activity using the startActivity method


Specifically, 
        Intent intent = new Intent(ShoulderExerciseScreen.this, ExercisesScreen.class);

        Bundle bundle = new Bundle();
        bundle.putSerializable("exercise", new Exercise("BenchPress));

        intent.putExtras(bundle);

        startActivity(intent);
in activity B
declare and object of the type you want passed
use the getIntent method  to get the intent
use the getSerializeableExtra to get the the value using the key
cast the returned value from getSerializeableExtra and the desired type
    Specifically,
        Exercise ex = (Exercise)getIntent().getSerializableExtra("exercise");

        if(ex != null)
        {
            //do something
        }
试试这个

ParcelTest.java

import java.util.HashMap;

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

public class ParcelTest implements Parcelable {
    private HashMap map;

    public ParcelTest() {
        map = new HashMap();
    }

    public ParcelTest(Parcel in) {
        map = new HashMap();
        readFromParcel(in);
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(map.size());
        for (String s: map.keySet()) {
            dest.writeString(s);
            dest.writeString(map.get(s));
        }
    }

    public void readFromParcel(Parcel in) {
        int count = in.readInt();
        for (int i = 0; i < count; i++) {
            map.put(in.readString(), in.readString());
        }
    }

    public String get(String key) {
        return map.get(key);
    }

    public void put(String key, String value) {
        map.put(key, value);
    }
}
ExampleCummeractivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ExampleSupplierActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ParcelTest p = new ParcelTest();
        p.put("green", "go");
        p.put("yellow", "wait");
        p.put("red", "stop");

        Bundle b = new Bundle();
        b.putParcelable("com.example.trafficlight", p);

        Intent i = new Intent(this, ExampleConsumerActivity.class);
        i.putExtras(b);

        startActivity(i);
    }
}
import android.app.Activity;
import android.os.Bundle;

public class ExampleConsumerActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getIntent().getExtras();

        ParcelTest p = b.getParcelable("com.example.trafficlight");

        String red = p.get("red");
        // ...
    }
}

请尝试详细说明你的答案,并提供一个基本的例子(例如从你的链接中复制并参考来源)。否则,如果链接不正确,你的答案一文不值。他只是传递整数,那么我认为没有必要为它创建一个可分组的类@ρ∑ρρK我认为他可以用这个例子来解决这个问题,答案将对更多的情况有用……你在活动之间共享数据的所有方式都是错误的,因为你试图访问活动中的一个活动方法其他活动和第二点是当您想要共享非自定义数据类型,如String.Integer、Float。。。在使用intent或bundle的应用程序之间,则无需实现parcelable接口。运行此代码时出现任何错误?您没有初始化
MyActivityParams
bundle。在
MainActivity
活动中使用
MyActivityParams
bundle之前,将其初始化为:
bundle MyActivityParams=new bundle()