【问题标题】:How to pass class by Interface to AsyncTask?如何通过接口将类传递给 AsyncTask?
【发布时间】:2018-07-11 15:28:26
【问题描述】:

我遇到了一点架构问题。

我创建了一个名为 BaseActivity.java 的抽象 Activity 类 在这个活动中,我把我的业务逻辑登录调用等。

这是基本的 BaseActivity 代码:

abstract class BaseActivity extends AppCompatActivity implements BaseActivityInterface {

      protected ApiParameters apiParameters;
      private BaseActivityInterface calledActivity;

      protected void getLoggedUser(BaseActivityInterface calledActivity) {
                this.calledActivity = calledActivity;
                GetLoggedUserTask getLoggedUserTask = new GetLoggedUserTask(this.apiParameters, BaseActivity.this);
                getLoggedUserTask.execute(Utility.LOGIN_URL);
      }

      @Override
      public void updateActivity(JSONObject response) throws JSONException {
          if (response.getInt("status") == HttpURLConnection.HTTP_OK) {
              this.initUserObject(response);
              if (this.calledActivity != null) {
                  this.calledActivity.updateActivity(response);
              }
          } else {
              Toast.makeText(this, response.getString("message"), Toast.LENGTH_LONG).show();
              Intent intent = new Intent(this, LoginActivity.class);
              this.startActivity(intent);
              finish();
          }
     }
}

这是名为 GetLoggedUserTask 的 AsyncTask:

public class GetLoggedUserTask extends  AsyncTask<String, BaseActivityInterface, String> {
    private ApiParameters apiParameters;
    private BaseActivityInterface response;

    public GetLoggedUserTask(ApiParameters apiParameters, BaseActivityInterface response) {
        this.apiParameters = apiParameters;
        this.response = response;
    }

这是我的 BaseActivity 的子类 CustomerActivity

public class CustomersActivity extends BaseActivity implements BaseActivityInterface {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.getLoggedUser(CustomersActivity.this);
    }


    @Override
    public void updateActivity(JSONObject loginResult) throws JSONException {
        .....
    }
}

最后是我的接口 BaseActivityInterface

public interface BaseActivityInterface extends Serializable {
    void updateActivity(JSONObject loginResult) throws JSONException;
}

流程应该是:启动 CustomerActivity.java,传递 super 以执行 getLoggetUser() 并从 super BaseActivity 启动 AsyncTask。这是热点:

当我传入 asyncTask 构造“BaseActivity.this”并进行调试时,GetLoggedUserTask 中“response”元素的引用被引用到子 CustomerActivity:

Class reference

我哪里做错了?

【问题讨论】:

    标签: java android inheritance mobile android-asynctask


    【解决方案1】:

    没有错。 BaseActivity 是一个 abstract 类,这意味着它不能被实例化。 BaseActivity.this 将返回扩展BaseActivity 的具体类的实例,在本例中为CustomersActivity。您永远不会将 BaseActivity.this 视为实例,因为在 java 中不可能拥有抽象类的实例。

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      相关资源
      最近更新 更多