当前位置: 首页 > 知识库问答 >
问题:

AsyncTask已停止调用doInBackground

谷梁迪
2023-03-14

在过去的几天里,我一直在使用这段代码,直到今天,它一直工作得很好,但由于某种原因,异步任务已经停止调用doInBackground方法。我尝试了这里建议的解决方案,Android SDK AsyncTask doInBackground没有运行(子类),但我得到了相同的结果。onPreExecute方法被调用,但我只剩下加载对话框。有没有人经历过类似的事情。我已经附上了我的代码副本。MyAsncTask的执行如下:;

新建MyAsyncTask()。执行(电子邮件、密码);

 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();
     }
  }

共有1个答案

爱博达
2023-03-14

试着像这样延伸

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

并使用@Override fordoInBackground

 类似资料:
  • 我运行我的Android应用程序(基于Java),它可以工作。接下来,我添加到我的应用程序代码: FirebaseFirestore fdb=FirebaseFirestore。getInstance(); 这是我从Android官方网站上得到的代码https://firebase.google.com/docs/firestore/quickstart 应用程序运行,但接下来运行的设备显示消息“

  • 问题内容: 我用来在JavaScript中每10秒调用一次函数。是否可以在某个事件中停止调用它? 我希望用户能够停止重复刷新数据。 问题答案: 返回一个间隔ID,您可以将其传递给: 请参阅该文档为和。

  • 在以前版本的Visual Studio(或者我几年前设置的一些设置)中,当我停止调试Web应用程序时,实际的IIS Express实例将继续运行。我真的很喜欢这种行为,因为这意味着当我编辑代码/重新编译ect时,站点总是在运行以进行修补。 自从升级到VS 2013后,这种情况似乎不再发生,当我停止调试时,它也停止了IIS express应用程序池。 有没有人知道某处有没有改变这种行为的设定?

  • 问题内容: 有时,当我调用Selenium FindElements(By)时,它将引发异常,并且驱动程序停止工作。参数“ BY”可能是问题所在:当我使用其他BY搜索相同的元素时,它起作用了。 我也可以看到,即使我的元素存在,或者之前曾调用过带有相同参数的相同方法,也不会阻止该方法引发异常。 我的方法是: 一个BY值的示例并非始终有效(即使它存在于页面中): 例外: WebDriverExcept

  • 我看了其他问题,但我无法澄清从另一个任务调用一个任务的疑问,我有以下代码: parseJSON是一个任务,当我运行程序时,它什么也不做,甚至不调用任务,这就像Android为了安全而忽略的一样。

  • 官方: http://developer.android.com/reference/android/os/asynctask.html onProgressUpdate(Progress...),在调用publishProgress(Progress...)后在UI线程上调用.执行的时间未定义。此方法用于在后台计算仍在执行时,在用户界面中显示任何形式的进度。例如,它可以用于动画进度条或在文本字段