【问题标题】:Casting ArrayList<File> into java.io.File Error将 ArrayList<File> 转换为 java.io.File 错误
【发布时间】:2020-04-22 02:45:20
【问题描述】:

我有一个 ArrayListImages

private final ArrayList<File> images;

似乎当我从Bundle 重新创建图像时,我得到了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;
}

日志显示,当我从Bundle 重新创建列表后尝试实际使用列表本身时出现错误:

    @Override
public void callRestApi(RestApi restApi, RestApiJobService.ApiRequestor apiRequestor) {

    File image = images.get(0); //This is the error line
}

但后来我收到了一个带有消息的Exception

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) 

【问题讨论】:

  • 好的,马上试试。但是 foreach 循环不起作用是有原因的吗?
  • @blackapps 尝试按照您的建议进行操作(编辑了问题),但我仍然遇到同样的错误
  • @blackapps 在这条线上File image = images.get(i)。我还尝试从循环中删除最后两行并得到相同的结果。
  • @blackapps 编辑了问题.. 似乎我在错误的地方寻找问题。

标签: android file arraylist casting


【解决方案1】:

所以基本上这里的答案是使用ArrayList&lt;String&gt; 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;
}

我不确定这是否是完全正确的方法,但它似乎可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 2014-05-12
    • 2013-06-03
    • 2018-08-11
    • 2019-07-19
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多