2016-02-13 180 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調用中。 –

相關問題