【发布时间】:2014-07-22 15:47:04
【问题描述】:
过去几天我一直在使用此代码,直到今天它一直运行良好,但由于某种原因,异步任务已停止调用 doInBackground 方法。我尝试了这里建议的解决方案Android SDK AsyncTask doInBackground not running (subclass),但我得到了相同的结果。 onPreExecute 方法被调用,但我只剩下加载对话框。有没有人经历过类似的事情。我已经包含了我的代码的副本。 MyAsncTask 执行如下;
new MyAsyncTask().execute(email, password);
private class MyAsyncTask extends AsyncTask<String, Void, JSONObject>
{
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
Log.v(tag, "onPreExecute");
super.onPreExecute();
pDialog = new ProgressDialog(SignInPageActivity.this);
pDialog.setMessage("Logging in...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
Log.v(tag, "doInBackground");
CustomerFunctions userFunction = new CustomerFunctions();
if (params.length != 2)
return null;
JSONObject json = userFunction.loginUser(params[0], params[1]);
return json;
}
@Override
protected void onPostExecute(JSONObject json)
{
Log.v(tag, "onPostExecute");
try {
if (json != null && json.getString(KEY_SUCCESS) != null) {
signInError.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("customer");
// Clear all previous data in database
CustomerFunctions userFunction = new CustomerFunctions();
userFunction.logoutCustomer(getApplicationContext());
databaseHandler.addUser(json.getString(KEY_UID), json_user.getString(KEY_FIRST_NAME), json_user.getString(KEY_LAST_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_CURRENT_STAMPS), json_user.getString(KEY_TOTAL_STAMPS), json_user.getString(KEY_REWARDS_AVAILABLE), json_user.getString(KEY_REWARDS_CLAIMED), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent main = new Intent(getApplicationContext(), MainActivity.class);
// Close all views before launching Dashboard
// dismiss the dialog once done
pDialog.dismiss();
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
// Close Registration Screen
finish();
}else{
// Error in login
signInError.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
你可以在
doInBackground上使用覆盖注释来排除编译时错误吗? -
您是否检查过 logcat 以确保
Log.v(tag, "doInBackground");实际上正在输出日志消息?还要检查未处理的异常堆栈跟踪。 -
Squonk 日志消息永远不会为 doInBackground 方法输出,并且似乎没有任何未处理的异常?