使用Bundle将对象从Activity发送到Fragment

时间:2018-07-25 08:13:15

标签: android object android-fragments fragment parcelable

我的“活动”中有2个对象(myProfile和employee),我想将其发送到Fragment。我使用了stackoverflow的捆绑软件,但它给了我错误。

我已经遵循下面的这个stackoverflow示例,但是失败了。 stackoverflow中的大多数示例都显示了如何传递STRING值 ...但我要发送对象

Send data from activity to fragment in android

https://medium.com/@hamidgh/sending-objects-to-fragment-naive-question-is-it-sent-by-value-ddaaa19fa42d

活动中

fragment = new MyProfile_FragmentActivity();
Bundle bundle = new Bundle();
bundle.putParcelable("myProfile", (Parcelable) myProfile);
bundle.putParcelable("employee", (Parcelable) employee);
fragment.setArguments(bundle);

在片段中

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {

    MyProfile myProfile = getArguments().getParcelable("myProfile");
    Employee employee = getArguments().getParcelable("employee");

    View rootView = inflater.inflate(R.layout.activity_my_profile, container, false);
    return rootView;
}

Employee.java

public class Employee implements Parcelable {

    //Retrieve from QRCODE
    private String id="";                              
    private String ip_address="";                      
    private Integer activated;

    //My Profile
    private String contact="";                          
    private String email="";                           
    private String name="kirpal";                      

    //Retrieve from device
    private String deviceID="";

    protected Employee(Parcel in) {
        id = in.readString();
        ip_address = in.readString();
        if (in.readByte() == 0) {
            activated = null;
        } else {
            activated = in.readInt();
        }
        contact = in.readString();
        email = in.readString();
        name = in.readString();
        deviceID = in.readString();
    }

    public static final Creator<Employee> CREATOR = new Creator<Employee>() {
        @Override
        public Employee createFromParcel(Parcel in) {
            return new Employee(in);
        }

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

    //SET
    public void set_id(String id){
        this.id = id;
    }

    public void set_ip_address(String ip_address){
        this.ip_address = ip_address;
    }



    //GET

    public String get_id(){
        return this.id;
    }

    public String get_ip_address(){
        return this.ip_address;
    }




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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(id);
        parcel.writeString(ip_address);
        if (activated == null) {
            parcel.writeByte((byte) 0);
        } else {
            parcel.writeByte((byte) 1);
            parcel.writeInt(activated);
        }
        parcel.writeString(contact);
        parcel.writeString(email);
        parcel.writeString(name);
        parcel.writeString(deviceID);
    }
}

错误日志

java.lang.ClassCastException: nextrackdomain.nextrack.MyProfile cannot be cast to android.os.Parcelable
at nextrackdomain.nextrack.MainActivity.onNavigationItemSelected(MainActivity.java:205)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:182)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:95)
at android.view.View.performClick(View.java:5181)
at android.view.View$PerformClick.run(View.java:20887)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

1 个答案:

答案 0 :(得分:1)

MyProfile和Employee都未实现Parcelable

困难的方法:让您的类按照documentation实施Parcelable。

简单方法:添加此lib。这将自动为您生成。只需要做

bundle.putParcelable("myProfile", Parcels.wrap(myProfile));
bundle.putParcelable("employee", Parcels.wrap(employee());

MyProfile myProfile = Parcels.unwrap(getArguments().getParcelable("myProfile"));
Employee employee = Parcels.unwrap(getArguments().getParcelable("employee"));

并在您的课程上添加@Parcel批注:

@Parcel
public class MyProfile{//rest of the normal code}

@Parcel
public class Employee{//rest of the normal code}