【问题标题】:AsyncTask has stopped calling doInBackgroundAsyncTask 已停止调用 doInBackground
【发布时间】: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 方法输出,并且似乎没有任何未处理的异常?

标签: android android-asynctask


【解决方案1】:

尝试像这样扩展

private class MyAsyncTask<Params, Void, JSONObject> extends AsyncTask<Params, Void, JSONObject>

并将@Override 用于doInBackground

【讨论】:

  • 感谢您的快速回复,我对 android 很陌生。当我将其更改为 Params 时,我得到一个错误 cannot resolve symbol Params,我是否需要做一些其他的事情?
  • 我试过了,但没有用,预执行运行的结果相同,我只剩下对话框了。为什么像上面那样扩展类时参数会变成蓝色?最令人沮丧的是前几天它运行良好。
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 2012-06-18
相关资源
最近更新 更多