質問

    @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?

役に立ちましたか?

解決

  • You post the Runnable to UI Thread in doInBackground() which results in pointless AsyncTask.
  • lstOutput is retuned before it is updated so it is always null.
  • if (lstOutput.size() == 0 || lstOutput == null) will crash if it's null when calling size(). Should check for null in first place.

The intention of your code is unclear, but you should probably do it like

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")){
    }
}

Edit: if the Handler is created in myReceipt() constructor, move it's creation to onPreExecute() or AsyncTasks constructor if it's called from UI thread. Anyway, myReceipt.saveReprint() method shouldn't use a Handler if it's executed in AsyncTask, so there's something totally wrong with your design.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top