将ArrayList<File>转换为java.io.File错误。

huangapple go评论67阅读模式
英文:

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&lt;File&gt; images;

It seems when I recreate the images from the Bundle I get the ClassCastException:

public DocumentJob(@Nullable ArrayList&lt;File&gt; images) {
    this.images = images;
}

public DocumentJob(Bundle bundle) {
    this.images = (ArrayList&lt;File&gt;) 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)&#160;
    at android.os.Looper.loop(Looper.java:214)&#160;
    at android.app.ActivityThread.main(ActivityThread.java:7156)&#160;
    at java.lang.reflect.Method.invoke(Native Method)&#160;
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)&#160;
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)&#160;

答案1

得分: 0

所以基本上答案是使用 ArrayList&lt;String&gt; imagePaths 而不是直接使用图像 images,然后在解包后,使用这些路径获取图像。

private ArrayList&lt;String&gt; imagePaths;

@Override
public Bundle createBundle() {
    bundle.putStringArrayList(BundleKeys.FILENAMES.name(), imagePaths);
    ...
}

public InstallationDocumentJob(Bundle bundle) {
    this.imagePaths = bundle.getStringArrayList(BundleKeys.FILENAMES.name());
    ArrayList&lt;File&gt; imageList = new ArrayList&lt;&gt;();
    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&lt;String&gt; imagePaths instead of the images themselves and then once they are unbundeled, get the images back using those paths.

private ArrayList&lt;String&gt; imagePaths;

    @Override
public Bundle createBundle() {
    bundle.putStringArrayList(BundleKeys.FILENAMES.name(), imagePaths);
    ...

public InstallationDocumentJob(Bundle bundle) {
    this.imagePaths = bundle.getStringArrayList(BundleKeys.FILENAMES.name());
    ArrayList&lt;File&gt; imageList = new ArrayList&lt;&gt;();
    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.

huangapple
  • 本文由 发表于 2020年1月3日 22:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定