Android 错误为asynctask onPostExecute
我需要一些帮助!在我的asyncTask中,我正在向ArrayList结果添加值,当我尝试在onPostExecute中获取结果值时Android 错误为asynctask onPostExecute,android,android-asynctask,Android,Android Asynctask,我需要一些帮助!在我的asyncTask中,我正在向ArrayList结果添加值,当我尝试在onPostExecute中获取结果值时 final String output = result.get(0); 应用程序正在强制关闭。我知道这可能是个愚蠢的错误。但我不明白为什么 public class deviceVerify extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
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);
}
我认为您试图在这里修改最后一个变量
result.add(“true”)代码>和这里的结果。添加(“false”)代码>更改
ArrayList<String> result = new ArrayList<String>;
//remove final from result
更改onpost方法
您获取此错误是因为您获取的结果大小为零。您正在“onSuccess”方法(回调方法)上的ArrayList“result”中添加数据。。但是在onSuccess之前,在PostExcecute方法上执行get,此时调用ArrayList“result”为空,这就是为什么会出现IndexOutofBoundException。final String output=result.get(0).toString();试试这个在OnSucess方法中完成onPostExcecute的所有内容我想你得到的是空的arraylist。那么arrayoutofbound Exception你确定在异步任务中需要它吗?似乎RestClientHelper
已经是异步的,这可以解释您的问题。这就是为什么它有RestClientListener
;完成后再打电话。@MikeM。是的,这是一项艰巨的任务,现在一切都好了!!是的,我想是的!!让我避免asynTask,并将onPostExcecute方法的所有代码置于“onSuccess”方法上。。并删除异步任务。
ArrayList<String> result = new ArrayList<String>;
//remove final from result
if(result != null && result.size()>0){
final String output = result.get(0);
}