当我自己运行应用程序时,没有这样的行为 - 一旦AsyncTask
完成执行,它就会返回到我的MainActivity(从第一位开始AsyncTask
)。
但是,在我的测试用例中,当AsyncTask
刚刚开始执行一小段时间时,MainActivity突然暂停,停止和销毁。
如何确保活动等待AsyncTask
将其回拨,就像我的应用程序的行为方式一样?
02-21 17:23:13.063: W/MainActivity(16685): onCreate
02-21 17:23:13.133: W/MainActivity(16685): refreshRequired
02-21 17:23:13.233: W/MainActivity(16685): user is not regitered
02-21 17:23:13.233: W/MainActivity(16685): onStart
02-21 17:23:13.233: W/MainActivity(16685): onResume
02-21 17:23:16.686: W/MainActivityFunctionalTest(16685): performing the button click action
02-21 17:23:17.227: W/MainActivity(16685): Submit button is clicked
02-21 17:23:17.377: W/Process(16685): doInBackground
02-21 17:23:17.377: W/RegistrationManager(16685): doRegistration
02-21 17:23:17.387: W/RegistrationManager(16685): sendJobRequest
02-21 17:23:17.487: W/RegistrationManager(16685): didOpenedAPDUConnection
02-21 17:23:17.507: W/MainActivity(16685): onPause
02-21 17:23:17.617: W/RegistrationManager(16685): sending HTTP request
02-21 17:23:17.617: W/MessageProcessor(16685): sending a HTTP request
02-21 17:23:17.627: W/HTTPRequest(16685): HTTPRequest.startAsynchronous timeout : 60000
02-21 17:23:17.627: W/HTTPRequest(16685): url is: ...
02-21 17:23:17.848: W/HTTPRequest(16685): HTTPRequest.readResponse RESP BODY = ca000000250104313030350218437573746f6d657220646f6573206e6f74206578697374210900000000
02-21 17:23:17.848: W/MessageProcessor(16685): requestFinished
02-21 17:23:17.858: W/RegistrationManager(16685): didFailedJobRequestWithResponseMsg
02-21 17:23:17.898: W/Process(16685): onPostExecute
02-21 17:38:29.057: W/MainActivity(17483): onStop
02-21 17:38:29.057: W/MainActivity(17483): onDestroy
我的测试班:
public class MainActivityFunctionalTest extends
ActivityInstrumentationTestCase2<MainActivity> {
public void testRegistrationFormIsSubmitted() {
final MainActivity mainActivity = (MainActivity) solo
.getCurrentActivity();
solo.assertCurrentActivity("current activity not main activity",
MainActivity.class);
final View submitBtn = solo.getView(R.id.btn_next);
assertNotNull(submitBtn);
Log.w(TAG, "performing the button click action");
solo.clickOnView(submitBtn);
}
}
MainActivity
ImageButton submitBtn = (ImageButton) findViewById(R.id.btn_next);
submitBtn.setImageResource(R.drawable.btn_submit);
// View submitBtn = findViewById(R.id.btn_next);
submitBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.w(TAG, "Submit button is clicked");
sendActivation(v);
}
});
private void sendActivation(View v) {
process = new Processor(this, this.getApplicationContext());
process.execute();
}
Processor.java
public class Processor extends AsyncTask<Void, String, Void>{
@Override
protected Void doInBackground(Void... arg0)
{
Log.w(TAG, "doInBackground");
mManager.doRegistration();
return null;
}
}
答案 0 :(得分:0)
protected Void doInBackground(Void... arg0)
你不应该做任何UI更改,甚至不显示Toasts
它会引发错误。
所以mManager.doRegistration();
如果你正在执行任何UI更改,它将抛出错误,
相反,如果有任何UI更改,您可以在protected void onPostExecute()
中使用它。