Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java.lang.ClassCastException,同时将Parcelable[]作为额外目的发送_Android_Android Intent - Fatal编程技术网

Android java.lang.ClassCastException,同时将Parcelable[]作为额外目的发送

Android java.lang.ClassCastException,同时将Parcelable[]作为额外目的发送,android,android-intent,Android,Android Intent,我将发送一个TutorAccount数组作为另一个活动的额外目的。以下是涉及的TutorAccount和Account类: public class TutorAccount extends Account{ public TutorAccount(TutorProfile profile) { super(profile); } public TutorAccount(TutorProfile profile, LocationInfo locati

我将发送一个
TutorAccount
数组作为另一个活动的额外目的。以下是涉及的TutorAccount和Account类:

public class TutorAccount extends Account{

    public TutorAccount(TutorProfile profile) {
        super(profile);
    }

    public TutorAccount(TutorProfile profile, LocationInfo locationInfo) {
        super(profile, locationInfo);
    }

    public TutorProfile getProfile() {
        return (TutorProfile) super.getProfile();
    }

    public void setProfile(TutorProfile profile) {
        super.setProfile(profile);
    }
}
以及Account类

public class Account implements Parcelable {

    /**Email Id of the User */
    private String mEmailID;

    /**Important to have the status indexed as this shall be used during a query for a particular user type*/
    private Integer accountStatus;

    // Profile info.
    private GenericLearnerProfile profile;

    // Tutor's location info. - this would be computed from {Latitude, Longitude} which shall be available
    // from the _1 info. Also, this holds a reference to the LatLng{Latitude, Longitude} from the _1.
    private LocationInfo locationInfo;

    public Account(GenericLearnerProfile profile) {
        if(profile == null){
            throw new NullPointerException("Profile can't be null");
        }
        this.profile = profile;
        mEmailID = profile.getEmailID();
        // Auto boxing
        this.accountStatus = profile.getCurrentStatus();
    }

    public Account(GenericLearnerProfile profile, LocationInfo locationInfo) {
        if(profile == null){
            throw new NullPointerException("Profile can't be null");
        }
        this.profile = profile;
        this.locationInfo = locationInfo;

        mEmailID = profile.getEmailID();

        this.accountStatus = profile.getCurrentStatus();
    }

    private Account(){

    }

    public GenericLearnerProfile getProfile() {
        return profile;
    }

    public void setProfile(GenericLearnerProfile profile) {
        if(profile == null){
            throw new NullPointerException("Profile can't be null");
        }
        this.profile = profile;
        mEmailID = profile.getEmailID();
    }

    public LocationInfo getLocationInfo() {
        return locationInfo;
    }

    public void setLocationInfo(LocationInfo locationInfo) {
        this.locationInfo = locationInfo;
    }

    public static class LocationInfo implements Parcelable {
        private String shortFormattedAddress;

        public LocationInfo(String shortFormattedAddress) {
            this.shortFormattedAddress = shortFormattedAddress;
        }

        public String getShortFormattedAddress() {
            return shortFormattedAddress;
        }

        public void setShortFormattedAddress(String shortFormattedAddress) {
            this.shortFormattedAddress = shortFormattedAddress;
        }

        protected LocationInfo(Parcel in) {
            shortFormattedAddress = in.readString();
        }

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

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(shortFormattedAddress);
        }

        @SuppressWarnings("unused")
        public static final Parcelable.Creator<LocationInfo> CREATOR = new Parcelable.Creator<LocationInfo>() {
            @Override
            public LocationInfo createFromParcel(Parcel in) {
                return new LocationInfo(in);
            }

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

    protected Account(Parcel in) {
        mEmailID = in.readString();
        accountStatus = in.readByte() == 0x00 ? null : in.readInt();
        profile = (GenericLearnerProfile) in.readValue(GenericLearnerProfile.class.getClassLoader());
        locationInfo = (LocationInfo) in.readValue(LocationInfo.class.getClassLoader());
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mEmailID);
        if (accountStatus == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(accountStatus);
        }
        dest.writeValue(profile);
        dest.writeValue(locationInfo);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Account> CREATOR = new Parcelable.Creator<Account>() {
        @Override
        public Account createFromParcel(Parcel in) {
            return new Account(in);
        }

        @Override
        public Account[] newArray(int size) {
            return new Account[size];
        }
    };
}
这是说,帐户不能铸造到图托拉帐户。我甚至不知道这个帐号是如何进入演员阵容的,因为它应该是可以分给TutorAccount cast的

以下是用于发送和接收额外目的的代码:

发送活动

// Show these accounts in a list view
Intent i = new Intent(this, SearchResultsActivity.class);
i.putExtra(SEARCHED_ACCOUNTS, refactorAccountsToArray(accounts));
startActivity(i);
List<TutorAccount> list = new ArrayList<>(10);
        for(Parcelable p : getIntent ().getParcelableArrayExtra(SEARCHED_ACCOUNTS)){
            list.add((TutorAccount)p);
        }
接收活动

// Show these accounts in a list view
Intent i = new Intent(this, SearchResultsActivity.class);
i.putExtra(SEARCHED_ACCOUNTS, refactorAccountsToArray(accounts));
startActivity(i);
List<TutorAccount> list = new ArrayList<>(10);
        for(Parcelable p : getIntent ().getParcelableArrayExtra(SEARCHED_ACCOUNTS)){
            list.add((TutorAccount)p);
        }

根据@Anhaytanaun的建议,即使是一个演员,一个可包裹的实体也需要一个创造者。我的代码现在可以使用CREATOR和其他可打包的方法。修订后的守则如下:

package com.learncity.tutor.account;

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

import com.learncity.generic.learner.account.Account;
import com.learncity.tutor.account.profile.model.TutorProfile;

/**
 * Created by DJ on 3/7/2017.
 */

public class TutorAccount extends Account{

    public TutorAccount(TutorProfile profile) {
        super(profile);
    }

    public TutorAccount(TutorProfile profile, LocationInfo locationInfo) {
        super(profile, locationInfo);
    }

    public TutorProfile getProfile() {
        return (TutorProfile) super.getProfile();
    }

    public void setProfile(TutorProfile profile) {
        super.setProfile(profile);
    }

    public TutorAccount(Parcel in) {
        super(in);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<TutorAccount> CREATOR = new Parcelable.Creator<TutorAccount>() {
        @Override
        public TutorAccount createFromParcel(Parcel in) {
            return new TutorAccount(in);
        }

        @Override
        public TutorAccount[] newArray(int size) {
            return new TutorAccount[size];
        }
    };
}
package com.learncity.tutor.account;
导入android.os.packet;
导入android.os.Parcelable;
导入com.learncity.generic.learner.account.account;
导入com.learncity.tutor.account.profile.model.TutorProfile;
/**
*由DJ于2017年3月7日创建。
*/
公共类TutorAccount扩展帐户{
公共TutorAccount(TutorProfile档案){
超级(个人资料);
}
公共TutorAccount(TutorProfile配置文件、LocationInfo LocationInfo){
超级(配置文件、位置信息);
}
公共TutorProfile getProfile(){
return(TutorProfile)super.getProfile();
}
公共无效设置配置文件(TutorProfile配置文件){
super.setProfile(profile);
}
公共TutorAccount(包裹中){
超级(in),;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
超级可写包裹(目的地、旗帜);
}
@抑制警告(“未使用”)
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共TutorAccount createFromParcel(地块中){
返回新的TutorAccount(在中);
}
@凌驾
公共TutorAccount[]新数组(整数大小){
返回新的TutorAccount[大小];
}
};
}

据我从您的代码中了解,您正在传递帐户对象(帐户)列表,但在接收活动中,您试图将它们强制转换为TutorAccount。由于对象不是TutorAccount类型,而是Account类型,因此会出现强制转换错误。
重构AccountsToArray(accounts)
返回什么?显示它的代码。@Anhaytanaun:我编写并检查了只与TutorAccount对话的代码,而不与Account对话的代码,正如您通过我在编辑中添加的方法所看到的那样。@VladMatvienko:发布了代码。我不是100%舒尔,但尝试将CREATOR、writeToParcel和parcel构造函数添加到TutorAccount类中。在我看来,Parcelable使用Account类中的一个,因此“未加速”对象将是Account类型,而不是TutorAccount。请更新TutorAccount类,以便我们可以看到您添加的代码。