我遇到了一个小的体系结构问题。
我创建了一个名为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:的异步任务
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((,然后从superBaseActivity启动AsyncTask。这是热点:
当我传入asyncTask构造"BaseActivity.this"并进行调试时,GetLoggedUserTask中"response"元素的引用被引用到子CustomerActivity:
类别参考
我哪里做错了?
没有任何问题。BaseActivity
是一个abstract
类,这意味着它不能被实例化。BaseActivity.this
将返回一个扩展BaseActivity
的具体类的实例,在本例中为CustomersActivity
。您永远不会将BaseActivity.this
视为实例,因为在java中不可能有抽象类的实例。