Android将Asynctask对象的实例传递给另一个活动

时间:2016-06-21 15:43:53

标签: java android serialization android-asynctask

我将自定义的Asynctask类传递给另一个活动时遇到问题。自定义类实现序列化。我把对象置于额外的意图但我不能通过getserializationextra在另一个活动中读取它。有异常没有序列化......如何传递这个对象?

修改

这是我的自定义类扩展AsyncTask:

  Intent passToProfil = new Intent(Login.this, Profil.class);
    passToProfil.putExtra("LOGIN_ASYNCTASK", loginAsyncTask);
    passToProfil.putExtra("NEW_USER", user);
    startActivity(passToProfil);
    finish();

我尝试将对象传递给另一个活动的Activity类:

Intent getU = getIntent();
    loginAsyncTask=(LoginAsyncTask)getU.getSerializableExtra("LOGIN_ASYNCTASK");

和我试图获取对象的活动:

06-21 20:11:01.771 29697-29809/com.mmc.mateusz.gymbuddies E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                        java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                                                                            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                            at java.lang.Thread.run(Thread.java:856)
                                                                         Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mmc.mateusz.gymbuddies.LoginAsyncTask)
                                                                            at android.os.Parcel.writeSerializable(Parcel.java:1279)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1233)
                                                                            at android.os.Parcel.writeMapInternal(Parcel.java:591)
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1619)
                                                                            at android.os.Parcel.writeBundle(Parcel.java:605)
                                                                            at android.content.Intent.writeToParcel(Intent.java:6814)
                                                                            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1928)
                                                                            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1426)
                                                                            at android.app.Activity.startActivityForResult(Activity.java:3446)
                                                                            at android.app.Activity.startActivityForResult(Activity.java:3407)
                                                                            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
                                                                            at android.app.Activity.startActivity(Activity.java:3617)
                                                                            at android.app.Activity.startActivity(Activity.java:3585)
                                                                            at com.mmc.mateusz.gymbuddies.Login.startProfil(Login.java:82)
                                                                            at com.mmc.mateusz.gymbuddies.Login.passUserToProfil(Login.java:124)
                                                                            at com.mmc.mateusz.gymbuddies.LoginAsyncTask.doInBackground(LoginAsyncTask.java:51)
                                                                            at com.mmc.mateusz.gymbuddies.LoginAsyncTask.doInBackground(LoginAsyncTask.java:16)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                            at java.lang.Thread.run(Thread.java:856) 
                                                                         Caused by: java.io.NotSerializableException: android.support.v7.widget.AppCompatButton
                                                                            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
                                                                            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
                                                                            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
                                                                            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
                                                                            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
                                                                            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
                                                                            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
                                                                            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
                                                                            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
                                                                            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
                                                                            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
                                                                            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
                                                                            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
                                                                            at android.os.Parcel.writeSerializable(Parcel.java:1274)
                                                                            at android.os.Parcel.writeValue(Parcel.java:1233) 
                                                                            at android.os.Parcel.writeMapInternal(Parcel.java:591) 
                                                                            at android.os.Bundle.writeToParcel(Bundle.java:1619) 
                                                                            at android.os.Parcel.writeBundle(Parcel.java:605) 
                                                                            at android.content.Intent.writeToParcel(Intent.java:6814) 
                                                                            at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1928) 
                                                                            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1426) 
                                                                            at android.app.Activity.startActivityForResult(Activity.java:3446) 
                                                                            at android.app.Activity.startActivityForResult(Activity.java:3407) 
                                                                            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843) 
                                                                            at android.app.Activity.startActivity(Activity.java:3617) 
                                                                            at android.app.Activity.startActivity(Activity.java:3585) 
                                                                            at com.mmc.mateusz.gymbuddies.Login.startProfil(Login.java:82) 
                                                                            at com.mmc.mateusz.gymbuddies.Login.passUserToProfil(Login.java:124) 
                                                                            at com.mmc.mateusz.gymbuddies.LoginAsyncTask.doInBackground(LoginAsyncTask.java:51) 
                                                                            at com.mmc.mateusz.gymbuddies.LoginAsyncTask.doInBackground(LoginAsyncTask.java:16) 
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:287) 
                                                                            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                            at java.lang.Thread.run(Thread.java:856) 

并且有例外:

        startActivity(passToProfil);

第82行是:

public class LoginAsyncTask extends AsyncTask<Void, Void, Void> implements Serializable {

第16行是:

{{1}}

我有另一个User.class对象,我以相同的方式实现Serialization并且没有问题。我需要AsyncTask对象来完成登录我的用户...所以我需要传递给另一个活动,以便将对象User传递给服务器。

0 个答案:

没有答案