@Override
protected List<String> doInBackground(String... urls) {
JPOSeReprintPreview.this.runOnUiThread(new Runnable() {
@Override
public void run() {
myReceipt reprintReceipt = new myReceipt ();
lstOutput= reprintReceipt.saveReprint(muncID);
if(lstOutput.size()==0 || lstOutput==null){
System.out.println("Failed Sending data");
}
}
});
return lstOutput;
}
@Override
protected void onPostExecute(List<String> response) {
if(response.get(0).equals("SUCCESS")){ } }
我的列表lstOutput访问onPostExecute时重置为null ..我的onPostExecute上有无效的索引异常..为什么会这样?
答案 0 :(得分:2)
Runnable
中将UI Thread
发布到doInBackground()
,结果毫无意义AsyncTask
。lstOutput
在更新之前会重新调整,因此它始终为空。if (lstOutput.size() == 0 || lstOutput == null)
时size()
为空,则会崩溃。应该首先检查是否为空。您的代码的意图尚不清楚,但您应该像
那样做private final myReceipt reprintReceipt;
public AsyncTaskName() {
myReceipt reprintReceipt = new myReceipt();
}
@Override
protected List<String> doInBackground(String... urls) {
return reprintReceipt.saveReprint(muncID);
}
@Override
protected void onPostExecute(List<String> response) {
if (response == null || response.isEmpty()){
System.out.println("Failed Sending data");
return;
}
if(response.get(0).equals("SUCCESS")){
}
}
编辑:如果在Handler
构造函数中创建了myReceipt()
,如果从UI线程调用它,则将其创建移动到onPreExecute()
或AsyncTask
构造函数。
无论如何,myReceipt.saveReprint()方法如果在AsyncTask中执行,则不应使用Handler
,因此您的设计存在完全错误。