英文:
Casting ArrayList<File> into java.io.File Error
问题
I have an ArrayList
of Images
private final ArrayList<File> images;
It seems when I recreate the images from the Bundle
I get the ClassCastException
:
public DocumentJob(@Nullable ArrayList<File> images) {
this.images = images;
}
public DocumentJob(Bundle bundle) {
this.images = (ArrayList<File>) bundle.getSerializable(BundleKeys.FILENAMES.name());
}
@Override
public Bundle createBundle() {
Bundle bundle = new Bundle();
bundle.putSerializable(BundleKeys.FILENAMES.name(), images);
return bundle;
}
The logs show that the error comes when I try to actually use the list itself after it is recreated from the Bundle
:
@Override
public void callRestApi(RestApi restApi, RestApiJobService.ApiRequestor apiRequestor) {
File image = images.get(0); //This is the error line
}
But then I get an Exception
with a message:
2020-01-03 16:08:39.606 1386-1386/example.example.com.myApplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.example.com.myApplication, PID: 1386
java.lang.RuntimeException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
at example.example.com.myApplication.model.DocumentJob.callRestApi(DocumentJob.java:82)
at example.example.com.myApplication.service.RestApiJobService.onStartJob(RestApiJobService.java:81)
at android.app.job.JobService$1.onStartJob(JobService.java:62)
at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
英文:
I have an ArrayList
of Images
private final ArrayList<File> images;
It seems when I recreate the images from the Bundle
I get the ClassCastException
:
public DocumentJob(@Nullable ArrayList<File> images) {
this.images = images;
}
public DocumentJob(Bundle bundle) {
this.images = (ArrayList<File>) bundle.getSerializable(BundleKeys.FILENAMES.name());
}
@Override
public Bundle createBundle() {
Bundle bundle = new Bundle();
bundle.putSerializable(BundleKeys.FILENAMES.name(), images);
return bundle;
}
The logs show that the error comes when I try to actually use the list itself after it is recreated from the Bundle
:
@Override
public void callRestApi(RestApi restApi, RestApiJobService.ApiRequestor apiRequestor) {
File image = images.get(0); //This is the error line
}
But then I get an Exception
with a message:
2020-01-03 16:08:39.606 1386-1386/example.example.com.myApplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.example.com.myApplication, PID: 1386
java.lang.RuntimeException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:112)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.io.File
at example.example.com.myApplication.model.DocumentJob.callRestApi(DocumentJob.java:82)
at example.example.com.myApplication.service.RestApiJobService.onStartJob(RestApiJobService.java:81)
at android.app.job.JobService$1.onStartJob(JobService.java:62)
at android.app.job.JobServiceEngine$JobHandler.handleMessage(JobServiceEngine.java:108)
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7156) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 
答案1
得分: 0
所以基本上答案是使用 ArrayList<String> imagePaths
而不是直接使用图像 images
,然后在解包后,使用这些路径获取图像。
private ArrayList<String> imagePaths;
@Override
public Bundle createBundle() {
bundle.putStringArrayList(BundleKeys.FILENAMES.name(), imagePaths);
...
}
public InstallationDocumentJob(Bundle bundle) {
this.imagePaths = bundle.getStringArrayList(BundleKeys.FILENAMES.name());
ArrayList<File> imageList = new ArrayList<>();
assert imagePaths != null;
for (String imagePath: imagePaths) {
if (imagePath != null) {
imageList.add(new File(imagePath));
}
}
this.images = imageList;
}
我不确定这是否是完全正确的方法,但它似乎可以解决问题。
英文:
So basically the answer here was to use ArrayList<String> imagePaths
instead of the images
themselves and then once they are unbundeled, get the images back using those paths.
private ArrayList<String> imagePaths;
@Override
public Bundle createBundle() {
bundle.putStringArrayList(BundleKeys.FILENAMES.name(), imagePaths);
...
public InstallationDocumentJob(Bundle bundle) {
this.imagePaths = bundle.getStringArrayList(BundleKeys.FILENAMES.name());
ArrayList<File> imageList = new ArrayList<>();
assert imagePaths != null;
for (String imagePath: imagePaths) {
if (imagePath != null) {
imageList.add(new File(imagePath));
}
}
this.images = imageList;
}
I am not sure if this is the completely correct approach, but it seems to do the trick.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论