我需要一些帮助!在我的asyncTask中,我正在向ArrayList Result添加值,当我尝试通过
获取onPostExecute中的结果值时final String output = result.get(0);
该应用程序正在关闭。我知道这可能是一个愚蠢的错误。但我不明白为什么!
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);
}
LOG ERROR:
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 :(得分:1)
更改
ArrayList<String> result = new ArrayList<String>;
//remove final from result
也改变它不要求,但仅限于标准编码
if(result != null && result.size()>0){
final String output = result.get(0);
}
改变onpost方法 你得到这个错误,因为你得到的结果大小为零
答案 1 :(得分:1)
您正在“onSuccess”方法上添加ArrayList “result”中的数据,这是回调方法..但是在onSuccess执行之前,onPostExcecute方法会在此时调用,然后调用ArrayList “result “是空的,这就是为什么IndexOutOfBoundExeption ouckured。
答案 2 :(得分:0)
我认为您尝试在result.add("true");
和result.add("false");