@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")){ } }
My list lstOutput Reset to null when accessing the onPostExecute.. im having invalid index exception at my onPostExecute.. why this is happening?
最佳答案
- 您将
Runnable
发布到doInBackground()
中的UI线程
,这会导致毫无意义的AsyncTask
。< lstOutput
在更新之前会重新调整,因此它始终为 null。if (lstOutput.size() == 0 || lstOutput == null)
如果调用size()
时为 null,将会崩溃。应首先检查 null。
您的代码的 Intent 尚不清楚,但您应该这样做
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()
构造函数中创建的,请将其创建移动到 onPreExecute()
或 AsyncTask
s 构造函数(如果从 UI 线程调用)。
无论如何,如果 myReceipt.saveReprint() 方法在 AsyncTask 中执行,则它不应使用 Handler
,因此您的设计完全错误。
关于java - 使用 runOnUiThread 时,异步任务将 List 重置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605856/