2016-02-13 121 views
0

运行以下代码时,我得到的数组超出了索引错误。致命异常:AsyncTask#3(执行doInBackground()时发生错误)

public class GetImageFileTask extends AsyncTask { 

    private OnGetImageFileListener listener; 

    public GetImageFileTask(OnGetImageFileListener listener) { 
     this.listener = listener; 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 
     File imageFile = null; 
     ImageHelper imageHelper = (ImageHelper) params[0]; 
     ImageView imageView = (ImageView) params[1]; 

     try { 
      imageFile = imageHelper.getFileFromImageView(imageView); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return imageFile; 
    } 

    @Override 
    protected void onPostExecute(Object imageFile) { 
     listener.onGotImageFile((File) imageFile); 
    } 
} 

我不明白为什么。此代码在另一个项目中工作,但不在我目前正在使用的项目中。这是错误日志。

02-13 17:20:21.073 30900-30985/app.protoype3 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4 
Process: app.protoype3, PID: 30900 
java.lang.RuntimeException: An error occured while executing doInBackground() 
     at android.os.AsyncTask$3.done(AsyncTask.java:300) 
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
     at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 
     at app.protoype3.GetImageFileTask.doInBackground(GetImageFileTask.java:24) 
     at android.os.AsyncTask$2.call(AsyncTask.java:288) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 
+3

请显示您调用'AsyncTask'的代码 – Rohit5k2

+0

'AsyncTask'有3个类型参数,而您使用的是原始类型。你也用一个接受Object []的方法重写了一个可变参数方法'doInBackground(Params ... params)'。这是一个巨大的混乱(尽管我不认为这与你的实际问题有任何关系)的秘诀。 –

回答

0
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 

也许错误是,你PARAMS有1长?你如何执行你的AsyncTask?您需要致电task.execute(imageHelper, imageView)

+0

谢谢。这帮了很多。我忘了将imageView部件添加到我的task.execute调用中。 –

相关问题