2017-02-22 81 views
-2

我需要一些幫助!在我的AsyncTask我增加值ArrayList的結果,當我試圖通過錯誤是asynctask onPostExecute

final String output = result.get(0); 

獲取在onPostExecute結果值的應用程序是強制關閉。我知道這可能是一個愚蠢的錯誤。但我不明白爲什麼!

public class deviceVerify extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> { 
     protected void onPreExecute(){ 
      progress1.setVisibility(View.VISIBLE); 
     } 
     @Override 
     protected ArrayList<String> doInBackground(ArrayList<String>... passing) { 
      // Locate the WorldhavingDelay Class 
      // JSON file URL address 
      final ArrayList<String> result = new ArrayList<String>(); 
      String android_id = Settings.Secure.getString(getContentResolver(), 
        Settings.Secure.ANDROID_ID); 
      try { 
       Log.e("URL",Constants.BASE_URL_LOGIN+"?deviceid="+android_id); 
       RestClientHelper.getInstance().get(Constants.BASE_URL_LOGIN+"?deviceid="+android_id, new RestClientHelper.RestClientListener() { 
        @Override 
        public void onSuccess(String response) { 
         Log.e("Resposnse",response); 
         result.add("true"); 
        } 

        @Override 
        public void onError(String error) { 
         Log.e("Error",error); 
         result.add("false"); 
        } 
       }); 
      } catch (Exception e) { 
       result.add("false"); 
      } 
      return result; 
     } 
     protected void onPostExecute(final ArrayList<String> result) { 
      final String output = result.get(0); 
      final Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        progress1.setVisibility(View.INVISIBLE); 
        if(output.equals("true")){ 
         TextView verifytext = (TextView) findViewById(R.id.verifytext); 
         verifytext.setText("Device Verified"); 
         ImageView error = (ImageView) findViewById(R.id.messageImage); 
         error.setImageResource(DeviceVerification.this.getResources().getIdentifier("check", "drawable", DeviceVerification.this.getPackageName())); 
        } 
        else{ 
         TextView verifytext = (TextView) findViewById(R.id.verifytext); 
         verifytext.setText("An Error Occured on verifying device!"); 
         ImageView error = (ImageView) findViewById(R.id.messageImage); 
         error.setImageResource(DeviceVerification.this.getResources().getIdentifier("warning", "drawable", DeviceVerification.this.getPackageName())); 
         Button tryagain = (Button) findViewById(R.id.trygain); 
         tryagain.setVisibility(View.VISIBLE); 
        } 

       } 
      }, 10000); 
     } 

日誌錯誤:

02-22 15:06:34.708 10664-10664/in.juasoft.mobeeload E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: in.juasoft.mobeeload, PID: 10664 
                     java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
                      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
                      at java.util.ArrayList.get(ArrayList.java:308) 
                      at in.juasoft.mobeeload.DeviceVerification$deviceVerify.onPostExecute(DeviceVerification.java:110) 
                      at in.juasoft.mobeeload.DeviceVerification$deviceVerify.onPostExecute(DeviceVerification.java:78) 
                      at android.os.AsyncTask.finish(AsyncTask.java:632) 
                      at android.os.AsyncTask.access$600(AsyncTask.java:177) 
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5037) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                      at dalvik.system.NativeStart.main(Native Method) 
+0

final String output = result.get(0).toString();試試這個 –

+0

在OnSuceess方法中執行onPostExcecute的所有東西 –

+0

爲什麼接受字符串結果? – FreedomPride

回答

0

我覺得你在這裏嘗試修改最後的變量result.add("true");這裏result.add("false");

1

變化

ArrayList<String> result = new ArrayList<String>; 
//remove final from result 

也改變它不需要但僅限於展位AR作弄

if(result != null && result.size()>0){ 
     final String output = result.get(0); 
    } 

變化對onPOST等方法 因爲u得到結果的大小零

1

您在ArrayList中「結果」在其上的回調方法「的onSuccess」的方法將數據添加你流汗此錯誤。 。但onSuccess之前得到執行onPostExcecute方法當時被調用ArrayList 「結果」是空的這就是爲什麼IndexOutOfBoundExeption加密。

+0

是的,我認爲是這樣!讓我避開asynTask並檢查 –

+0

將onPostExcecute方法的所有代碼放在「onSuccess」方法上,然後刪除AsyncTask。 –