简体   繁体   中英

Passing ArrayList<Custom Object> from AsyncTask (Inside Service via Intent )to Activity

I have an AsyncTask inside an Service which is trying to pass a ArrayList to my Activity.

ArrayList<Malware> fetch = new ArrayList<Malware>();
        @Override
        protected void onPostExecute(Void result) {

                Intent d = new Intent();
                d.setAction(COMPLETED_INTENT);
                d.putExtra("CurrentProgress", "completed");
                d.putExtra("MalwareArray", fetch);
                sendBroadcast(d);
        }

I hit into an error while trying to broadcast the arraylist to the activity.

11-06 17:58:13.880: E/AndroidRuntime(13645): FATAL EXCEPTION: main
11-06 17:58:13.880: E/AndroidRuntime(13645): java.lang.RuntimeException: Parcel: unable to marshal value com.analyze.project.Malware@414e0510
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Parcel.writeValue(Parcel.java:1137)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Parcel.writeList(Parcel.java:524)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Parcel.writeValue(Parcel.java:1097)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Parcel.writeMapInternal(Parcel.java:493)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Bundle.writeToParcel(Bundle.java:1612)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Parcel.writeBundle(Parcel.java:507)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.content.Intent.writeToParcel(Intent.java:6216)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:1911)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:961)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:301)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at com.analyze.project.ScanService$FullScan.onPostExecute(ScanService.java:153)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at com.analyze.project.ScanService$FullScan.onPostExecute(ScanService.java:1)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.AsyncTask.finish(AsyncTask.java:602)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.os.Looper.loop(Looper.java:137)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at android.app.ActivityThread.main(ActivityThread.java:4513)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:741)
11-06 17:58:13.880: E/AndroidRuntime(13645):    at dalvik.system.NativeStart.main(Native Method)

You need to make your object implement the parcelable interface, so that you will be able to pass it via an intent.

Here is a good example of implementing this:

Android Parcelable -- RetailerOrderActivity.java return null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM