我需要一些幫助!在我的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)
final String output = result.get(0).toString();試試這個 –
在OnSuceess方法中執行onPostExcecute的所有東西 –
爲什麼接受字符串結果? – FreedomPride