可分为2类android

时间:2015-11-21 10:12:12

标签: android inheritance android-intent parcelable

我在android中还是新手,所以我对parcelable有点迷惑。所以这是我的问题: 我有3个班,分别是A,B,C。 B和C都从A 扩展。 我已经实现了其中三个" Parcelable"并且具有来自A的所有属性.B和C还具有A类旁边的另一个属性。

所以在我的MainActivity中,我想分开包装它们,因为首先我想让 A作为默认来使用并将数据加载到另一个活动,但是在另一个活动中,我向用户提供了一些选项,无论他们是否仍想使用A的数据,或者想要将其更改为B或C的数据

首先,我认为用户将无法返回主要活动(因此实际上有超过2个活动)。我打算从MainActivity传递B和C的数据,所以我不需要再次调用它。

这是我的代码: A类(父母)

public class A implements Parcelable{
private String name;
private int lvl;
private String job;

public A (String name, int lvl, String job)
{
    this.name = name;
    this.lvl = lvl;
    this.job = job;
}


//setter & getter
public void setName (String name) {this.name = name;}
public String getName () {return name;}

public void setLvl (int lvl) {this.lvl = lvl;}
public int getLvl () {return lvl;}

public void setJob (String job) {this.job = job;}
public String getJob () {return job;}

//Parcelable
protected A (Parcel source)
{
    this.name = source.readString ();
    this.lvl = source.readInt ();
    this.job = source.readString ();
}

public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>()
{
    public A createFromParcel (Parcel source) {return new A(source);}
    public A[] newArray (int size) {return new A[size];}
};

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

@Override
public void writeToParcel (Parcel source, int flags)
{
    source.writeString (name);
    source.writeInt (lvl);
    source.writeString (job);
} }

这是B和C代码(实际上它们具有相同的属性。只有它们的工作名称彼此不同)

    public class B extends A implements Parcelable
{
    private int job_lvl;
    private int exp;

    //constructor
    B (String name, int lvl, String job, int job_lvl, int exp)
    {
        super (name, lvl, job);
        this.job_lvl = job_lvl;
        this.exp = exp;
    }

    public void setJob_lvl (int job_lvl) {this.job_level = job_lvl;}
    public int getJob_lvl () {return job_lvl;}

    public void setExp (int exp) {this.exp = exp;}
    public int getExp () {return exp;}

    //Parcelable
    protected B (Parcel source)
    {
        super (source);
        job_lvl = source.readInt();
        exp = source.readInt();
    }

    @Override
    public void writeToParcel (Parcel source, int flags)
    {
        source.writeInt (job_lvl);
        source.writeInt (exp);
    }

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

    public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>()
    {
        @Override
        public B createFromParcel(Parcel source) {return new B(source);}

        @Override
        public B[] newArray(int size) {return new B[size[];}
    };
}
MainActivity中的

    public class MainActicity extends Activity
{
    Bundle bundle = new Bundle();
    A a;
    B b;
    //C c;

    String name;

    @Override
    protected void onCreate (Bundle saveInstanceState)
    {
        super.onCreate(savedInstanceState);
        //getText from user (input their name)
        //Name -> EditText

        name = Name.getText().toString();
        a = new A("", 1, "Novice");
        a.setName(name);
        //B (name, lvl, job, job_lvl, exp)
        bundle = new B(a.getName(), a.getLvl(), "Swordman", 1, 0);
        bundle.putParcelable ("Player", a);
        bundle.putParcelable ("Swordman", b);
        startActivity(new Intent(MainActivity.this,SecondActivity).putExtras(bundle));
        finish();
    }
}
第二个活动中的

    public class SecondActicity extends Activity
{
    Bundle bundle;
    A a;
    B b;
    //C c;


    @Override
    protected void onCreate (Bundle saveInstanceState)
    {
        super.onCreate(savedInstanceState);

        bundle = getIntent().getExtras();
        a = bundle.getParcelable ("Player");

        //Displaying data from class A because it is a default

        //user make a choice in here whether they want to change their job or not

        //if they want to change then from class A change to class B's data
        if (//choose to become swordman)
        {
            b = bundle.getParcelable ("Swordman");
            //showing the data from class B
            //change all the activities which require A,B,C's data that depend on user's choice
        }

    }
}

我在这段代码中遇到错误:

a = bundle.getParcelable ("Player");

错误说:

FATAL EXCEPTION: main
Process: danbin.game, PID: 15577
java.lang.RuntimeException: Unable to start activity ComponentInfo{danbin.game/danbin.game.SecondActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@25a544dc: Unmarshalling unknown type code 6357079 at offset 212
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@25a544dc: Unmarshalling unknown type code 6357079 at offset 212
at android.os.Parcel.readValue(Parcel.java:2259)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2516)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:755)
at android.content.Intent.getParcelableExtra(Intent.java:5089)
at danbin.game.PlayActivity.onCreate(SecondActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
at android.app.ActivityThread.access$800(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5373) 
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:1020) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
你帮我解决了这个问题吗?我已经在3天前到处搜索,但仍无法找到解决方案。

我真的很感谢你的帮助! 谢谢

0 个答案:

没有答案