在匿名对象上调用超类方法 AsyncTask.execute() new HttpRequestTask<...> extends AsyncTask<...>
Calling superclass method AsyncTask.execute() on anonymous object new HttpRequestTask<...> extends AsyncTask<...>
我在创建匿名对象和调用超类中实现的函数时遇到问题。
异步任务
public abstract class AsyncTask<Params, Progress, Result>{
protected abstract Result doInBackground(Params... params);
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
//implementation. Shortly, it calls doInBackground(params).
}
}
HttpRequestTask
public abstract class HttpRequestTask<T> extends AsyncTask<String, Void, AsyncTaskResult<ResponseEntity<T>>>{
@Override
protected AsyncTaskResult<ResponseEntity<T>> doInBackground(String... url) {
//implementation
}
//...create other abstract methods (no relation with the question)...
public HttpRequestTask authenticate(SharedPreferences authPreferences) {
this.authPreferences = authPreferences;
return this;
}
}
我这样使用 类:
new HttpRequestTask<Player, Void>(Player.class) {
//...implementation of abstract methods from HttpRequestTask
}
.authenticate(authPreferences)
.execute("any string");
运行 这个,我得到这个异常:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
at com.edu.antechamber.controller.HttpRequestTask.doInBackground(HttpRequestTask.java:41)
第 41 行是:public abstract class HttpRequestTask<T> extends AsyncTask<String, Void, AsyncTaskResult<ResponseEntity<T>>>
应该说doInBackground()
里面的代码没有执行,因为在执行第一行之前就出现了异常
我认为当我创建匿名对象 new HttpRequestTask<Player, Void>(Player.class) {...}
、调用 authenticate(...)
、这个 returns 这个(匿名对象)并调用 execute("any string")
时就会出现问题。 execute()
应该调用 doInBackground("any string")
,但我认为它会调用一些 Object
的 doInBackground。
Android Studio 给我这个警告:
Unchecked Call to 'execute(Params…)' as a member of raw type 'android.os.AsyncTask'
如果我删除 .authenticate(authPreferences)
部分,应该不会有任何区别,因为 authenticate()
returns this
,但违反直觉一切正常。
以同样的方式,如果我在 HttpRequestTask
的构造函数中初始化 authPreferences
,它会再次运行。但我真的需要让 authenticate()
函数正常工作,否则我会有很多构造函数。
如果您能帮助我解决这个问题,我将不胜感激。
虽然可以通过传递数组来解决
execute(new String[]{"/api/player/55707e1eb7396de50dcef73d"})
而不是一个字符串参数
execute("/api/player/55707e1eb7396de50dcef73d")
问题出在这一行,而不是
public HttpRequestTask authenticate(SharedPreferences authPreferences)
如@user所说,应该是
public HttpRequestTask<T> authenticate(SharedPreferences authPreferences)
非常感谢!
我在创建匿名对象和调用超类中实现的函数时遇到问题。
异步任务
public abstract class AsyncTask<Params, Progress, Result>{
protected abstract Result doInBackground(Params... params);
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
//implementation. Shortly, it calls doInBackground(params).
}
}
HttpRequestTask
public abstract class HttpRequestTask<T> extends AsyncTask<String, Void, AsyncTaskResult<ResponseEntity<T>>>{
@Override
protected AsyncTaskResult<ResponseEntity<T>> doInBackground(String... url) {
//implementation
}
//...create other abstract methods (no relation with the question)...
public HttpRequestTask authenticate(SharedPreferences authPreferences) {
this.authPreferences = authPreferences;
return this;
}
}
我这样使用 类:
new HttpRequestTask<Player, Void>(Player.class) {
//...implementation of abstract methods from HttpRequestTask
}
.authenticate(authPreferences)
.execute("any string");
运行 这个,我得到这个异常:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
at com.edu.antechamber.controller.HttpRequestTask.doInBackground(HttpRequestTask.java:41)
第 41 行是:public abstract class HttpRequestTask<T> extends AsyncTask<String, Void, AsyncTaskResult<ResponseEntity<T>>>
应该说doInBackground()
里面的代码没有执行,因为在执行第一行之前就出现了异常
我认为当我创建匿名对象 new HttpRequestTask<Player, Void>(Player.class) {...}
、调用 authenticate(...)
、这个 returns 这个(匿名对象)并调用 execute("any string")
时就会出现问题。 execute()
应该调用 doInBackground("any string")
,但我认为它会调用一些 Object
的 doInBackground。
Android Studio 给我这个警告:
Unchecked Call to 'execute(Params…)' as a member of raw type 'android.os.AsyncTask'
如果我删除 .authenticate(authPreferences)
部分,应该不会有任何区别,因为 authenticate()
returns this
,但违反直觉一切正常。
以同样的方式,如果我在 HttpRequestTask
的构造函数中初始化 authPreferences
,它会再次运行。但我真的需要让 authenticate()
函数正常工作,否则我会有很多构造函数。
如果您能帮助我解决这个问题,我将不胜感激。
虽然可以通过传递数组来解决
execute(new String[]{"/api/player/55707e1eb7396de50dcef73d"})
而不是一个字符串参数
execute("/api/player/55707e1eb7396de50dcef73d")
问题出在这一行,而不是
public HttpRequestTask authenticate(SharedPreferences authPreferences)
如@user所说,应该是
public HttpRequestTask<T> authenticate(SharedPreferences authPreferences)
非常感谢!