我在Android上有一个简单的AsyncTask类,我注意到即使我知道doInBackground返回负数,我总是将正面结果传递给onPostExecute方法。
请查看我的代码。我在调试时运行此代码,并确认我落在异常块上并返回false。但它没有反映在onPostExecute上。
public class AsyncSendOSCTask extends AsyncTask<OSCMessage, Void, Boolean> {
private OSCPortOut oscPortOut;
private Activity activity;
public AsyncSendOSCTask(Activity activity, OSCPortOut oscPortOut) {
this.activity = activity;
this.oscPortOut = oscPortOut;
}
@Override
protected Boolean doInBackground(OSCMessage... message) {
try {
this.oscPortOut.send(message[0]);
return Boolean.TRUE;
}
catch(Exception exp) {
return Boolean.FALSE;
}
}
@Override
protected void onPostExecute(final Boolean result) {
if(!result.booleanValue()) { // this is always true!!!
Toast.makeText(this.activity, "Error Sending OSC", Toast.LENGTH_SHORT).show();
}
}
}
我在这里缺少什么?谢谢你的帮助...
答案 0 :(得分:0)
如果您的问题仍然存在:
请试试这个:
公共类AsyncSendOSCTask扩展了AsyncTask {
私人OSCPortOut oscPortOut; private activity activity; boolean examplebool; //添加了examplebool
public AsyncSendOSCTask(Activity activity,OSCPortOut oscPortOut){ this.activity =活动; this.oscPortOut = oscPortOut; }
@Override protected Boolean doInBackground(OSCMessage ... message){ 试试{
this.oscPortOut.send(message[0]); examplebool=true; // added return(examplebool); //edited } catch(Exception exp) { examplebool=false; //added return(examplebool); //edited } }
@Override protected void onPostExecute(final Boolean result){ if(!result.booleanValue()){//这总是正确的!
Toast.makeText(this.activity,“Error Snding OSC”,Toast.LENGTH_SHORT).show(); }}
快速手动编码,未经测试,您也可以更改onPostExecute。