将同一类实例从一个活动传递到另一个活动时出现问题

发布于 2024-09-27 02:35:25 字数 2514 浏览 5 评论 0原文

我正在尝试将 SampleParcelable 类对象(例如 exampleObj)从我的 ClassA(当前)活动传递到 ClassB(新活动),但是当我记录对象值时,我在 ClassA 中创建的对象的值与我获得的值完全不同B 类。

ClassA :-

public class ClassA extends Activity
{
    private SampleParcelable sampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        sampleObj = new SampleParcelable();
        Log.d("Actual Reference Value", "\t" + sampleObj);

        Intent terminateActivity = new Intent( ClassA.this, ClassB.class );
        terminateActivity.putExtra("SampleValue", sampleObj);
        SampleParcelable readbackCi = terminateActivity.getParcelableExtra("SampleValue");
        Log.d("Retrieved Value", "\n\n\t" + readbackCi);    
    }
}

ClassB :-

public class ClassB extends Activity
{
    private SampleParcelable newSampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try {
            Intent intentObj = getIntent();
            Log.d("Intent Value", "intent: " + intentObj.toString());
            Log.d("Extra Values", "extras: " + intentObj.getExtras());

            newSampleObj = (SampleParcelable) intentObj.getParcelableExtra("SampleValue");

            Log.d("New Value", " " + newSampleObj.toString());
        } catch (Exception e) {
            Log.d("Exception in main", e.toString());
        }
    }
}

SampleParcelable :-

public class SampleParcelable implements Parcelable
{
    public SampleParcelable(Parcel in) {
    in.readParcelable(SampleParcelable.class.getClassLoader());
    }

    public SampleParcelable() {
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub  
    }

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

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

经过调试我猜,我知道我的对象值不同的 1 个原因,因为当时使用 getParcelableExtra() 检索 ClassB 中的对象时,调用了我的 SampleParcelable 类 createFromParcel 方法,该方法在内部创建了一个新的目的。也许我错了。

我没有得到任何解决方案,我只想在我的新类中使用相同的对象,以便我可以使用在我的 ClassA 活动中设置的该对象访问一些值。

提前致谢

I am trying to pass SampleParcelable class object say sampleObj from my ClassA (current) activity to ClassB (a new one), but when i log the objects value, the object's value which i am creating in ClassA is totally different from what i get in ClassB.

ClassA :-

public class ClassA extends Activity
{
    private SampleParcelable sampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        sampleObj = new SampleParcelable();
        Log.d("Actual Reference Value", "\t" + sampleObj);

        Intent terminateActivity = new Intent( ClassA.this, ClassB.class );
        terminateActivity.putExtra("SampleValue", sampleObj);
        SampleParcelable readbackCi = terminateActivity.getParcelableExtra("SampleValue");
        Log.d("Retrieved Value", "\n\n\t" + readbackCi);    
    }
}

ClassB :-

public class ClassB extends Activity
{
    private SampleParcelable newSampleObj;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try {
            Intent intentObj = getIntent();
            Log.d("Intent Value", "intent: " + intentObj.toString());
            Log.d("Extra Values", "extras: " + intentObj.getExtras());

            newSampleObj = (SampleParcelable) intentObj.getParcelableExtra("SampleValue");

            Log.d("New Value", " " + newSampleObj.toString());
        } catch (Exception e) {
            Log.d("Exception in main", e.toString());
        }
    }
}

SampleParcelable :-

public class SampleParcelable implements Parcelable
{
    public SampleParcelable(Parcel in) {
    in.readParcelable(SampleParcelable.class.getClassLoader());
    }

    public SampleParcelable() {
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub  
    }

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

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

After debugging I guess, I know 1 reason why my object values are different, because when retrieving object in ClassB using getParcelableExtra() at that time my SampleParcelable class createFromParcel method is called which internally creates a new object. May be i m wrong.

I am not getting any solution for this, i just want same object in my new class so that i can access some values using that object which were set in my ClassA activity.

Thanks in advance

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

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

发布评论

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

评论(1

与酒说心事 2024-10-04 02:35:25

在这里,您如何实现您的意图::

package com.unundoinc.FaceBook.Activity;

import android.os.Parcel;

import android.os.Parcelable;

 public class CheckParcelable implements Parcelable

{

private Facebook facebook;

public CheckParcelable() { ; }

/**
 *
 * Constructor to use when re-constructing object
 * from a parcel
 *
 * @param in a parcel from which to read this object
 */
public CheckParcelable(Parcel in) {
    readFromParcel(in);
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) 
{
    // TODO Auto-generated method stub
    dest.writeValue(getFacebook());
}

private void readFromParcel(Parcel in) {

    // readParcelable needs the ClassLoader
    // but that can be picked up from the class
    // This will solve the BadParcelableException
    // because of ClassNotFoundException
    facebook = (Facebook) in.readValue(Facebook.class.getClassLoader());

}

public void setFacebook(Facebook facebook) {
    this.facebook = facebook;
}

public Facebook getFacebook() {
    return facebook;
}

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

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

}

对于使用 Parceable,您需要在需要将对象传递给其他 Activity 的类中执行类似的操作::

    Facebook facebook = new Facebook();

    facebook.setAccessToken("TIMEPASS");

    CheckParcelable parcelable = new CheckParcelable();

    parcelable.setFacebook(facebook);

    Intent newIntent = new Intent(ActivityA.this, ActivityB.class);

    newIntent.putExtra("CheckParcelable", parcelable);

并且为了从您需要执行的其他 Activity 获取对象这件事::

    CheckParcelable parcelable = getIntent().getExtras().getParcelable("CheckParcelable");

    Facebook facebook = parcelable.getFacebook();

    Log.v(TAG, "PARCELABLE IS ::" +facebook.getAccessToken());

我希望这能解决你的问题;D

Here how you can achieve what you intend for::

package com.unundoinc.FaceBook.Activity;

import android.os.Parcel;

import android.os.Parcelable;

 public class CheckParcelable implements Parcelable

{

private Facebook facebook;

public CheckParcelable() { ; }

/**
 *
 * Constructor to use when re-constructing object
 * from a parcel
 *
 * @param in a parcel from which to read this object
 */
public CheckParcelable(Parcel in) {
    readFromParcel(in);
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) 
{
    // TODO Auto-generated method stub
    dest.writeValue(getFacebook());
}

private void readFromParcel(Parcel in) {

    // readParcelable needs the ClassLoader
    // but that can be picked up from the class
    // This will solve the BadParcelableException
    // because of ClassNotFoundException
    facebook = (Facebook) in.readValue(Facebook.class.getClassLoader());

}

public void setFacebook(Facebook facebook) {
    this.facebook = facebook;
}

public Facebook getFacebook() {
    return facebook;
}

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

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

}

For Using the Parceable You Need to do something like this in the class from where you require pass the object to the other Activity::

    Facebook facebook = new Facebook();

    facebook.setAccessToken("TIMEPASS");

    CheckParcelable parcelable = new CheckParcelable();

    parcelable.setFacebook(facebook);

    Intent newIntent = new Intent(ActivityA.this, ActivityB.class);

    newIntent.putExtra("CheckParcelable", parcelable);

And for getting the Object from Other Activity you require to perform this thing ::

    CheckParcelable parcelable = getIntent().getExtras().getParcelable("CheckParcelable");

    Facebook facebook = parcelable.getFacebook();

    Log.v(TAG, "PARCELABLE IS ::" +facebook.getAccessToken());

I hope this would solve you problem ;D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文