您好我已将MyAsyncHttpRequest
任务实施为AsyncTask
,如下所示
public class MyAsyncHttpRequest extends AsyncTask<String, Void, ArrayList> {
private Activity activity;
private AsyncTaskCompleteListener callback;
private SessionManager session;
public MyAsyncHttpRequest(Activity act, SessionManager session ) {
try {
Log.i("TAG", "MyAsyncHttpRequest::Constructor");
this.activity = act;
this.callback = (AsyncTaskCompleteListener)act; // Just here crashes all!!!
this.session = session;
} catch ( Exception exc ) {
exc.printStackTrace();
}
... // More Stuff
@Override
protected void onPostExecute(ArrayList result) {
super.onPostExecute(result);
if (null != dialog && dialog.isShowing()) {
dialog.dismiss();
}
// Obviously, if callback couldn't be assigned in Constructor, here crashes too
callback.onTaskComplete(result, cmd);
}
}
和
这样的公共接口AsyncTaskCompleteListener
public interface AsyncTaskCompleteListener {
public void onTaskComplete(ArrayList list, int cmd);
}
以及我称之为MyAsyncHttpRequest
的活动
public class WmsMapActivity extends Activity implements AsyncTaskCompleteListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
myMethod();
}
private myMethod() {
...
myAsyncHttpRequest = new MyAsyncHttpRequest(this, session); // It crashes here before I do "myAsynHttpRequest.execute(...);"
...
}
@Override
public void onTaskComplete(ArrayList list, int cmd){
... // Some stuff
}
}
我的Logcat就在下面,它说,我的活动无法投射到AsyncTaskCompleteListener
com.nutiteq.advancedmap.activity.WmsMapActivity cannot be cast to com.utils.AsyncTaskCompleteListener
这种方法一直运行到今天早上,但现在我得到了这个例外。
任何人都可以告诉我我的代码中有什么问题吗?
非常感谢。
09-22 11:19:05.314: I/TAG(9310): MyAsyncHttpRequest::Constructor
09-22 11:19:05.324: W/System.err(9310): java.lang.ClassCastException:com.nutiteq.advancedmap.activity.WmsMapActivity cannot be cast to com.XXX.utils.AsyncTaskCompleteListener
09-22 11:19:05.324: W/System.err(9310): at com.XXX.utils.MyAsyncHttpRequest.<init>(MyAsyncHttpRequest.java:54)
09-22 11:19:05.324: W/System.err(9310): at com.nutiteq.advancedmap.activity.WmsMapActivity.tryToListFarms(WmsMapActivity.java:1349)
09-22 11:19:05.324: W/System.err(9310): at com.nutiteq.advancedmap.activity.WmsMapActivity.buildGroupListFromServer(WmsMapActivity.java:1368)
09-22 11:19:05.324: W/System.err(9310): at com.nutiteq.advancedmap.activity.WmsMapActivity.onCreate(WmsMapActivity.java:476)
09-22 11:19:05.324: W/System.err(9310): at android.app.Activity.performCreate(Activity.java:5231)
09-22 11:19:05.324: W/System.err(9310): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 11:19:05.324: W/System.err(9310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-22 11:19:05.324: W/System.err(9310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-22 11:19:05.324: W/System.err(9310): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-22 11:19:05.324: W/System.err(9310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-22 11:19:05.324: W/System.err(9310): at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 11:19:05.324: W/System.err(9310): at android.os.Looper.loop(Looper.java:136)
09-22 11:19:05.324: W/System.err(9310): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-22 11:19:05.324: W/System.err(9310): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 11:19:05.324: W/System.err(9310): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 11:19:05.324: W/System.err(9310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-22 11:19:05.324: W/System.err(9310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-22 11:19:05.324: W/System.err(9310): at dalvik.system.NativeStart.main(Native Method)
09-22 11:19:05.324: I/TAG(9310): tryToListFarms response:[]
09-22 11:19:05.324: I/TAG(9310): false
09-22 11:19:05.324: I/TAG(9310): MyAsyncHttpRequest::doInBackground 0
09-22 11:19:05.324: I/TAG(9310): MyAsyncHttpRequest::doInBackground session=null
09-22 11:19:05.364: I/TAG(9310): MyAsyncHttpRequest::onPostExecute:onPostExecute result: null
09-22 11:19:05.364: D/AndroidRuntime(9310): Shutting down VM
09-22 11:19:05.364: W/dalvikvm(9310): threadid=1: thread exiting with uncaught exception (group=0x4156cba8)
09-22 11:19:05.364: E/AndroidRuntime(9310): FATAL EXCEPTION: main
09-22 11:19:05.364: E/AndroidRuntime(9310): Process: com.XXX.start, PID: 9310
09-22 11:19:05.364: E/AndroidRuntime(9310): java.lang.NullPointerException
09-22 11:19:05.364: E/AndroidRuntime(9310): at com.XXX.utils.MyAsyncHttpRequest.onPostExecute(MyAsyncHttpRequest.java:108)
09-22 11:19:05.364: E/AndroidRuntime(9310): at com.XXX.utils.MyAsyncHttpRequest.onPostExecute(MyAsyncHttpRequest.java:1)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.os.AsyncTask.finish(AsyncTask.java:632)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.os.Looper.loop(Looper.java:136)
09-22 11:19:05.364: E/AndroidRuntime(9310): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-22 11:19:05.364: E/AndroidRuntime(9310): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 11:19:05.364: E/AndroidRuntime(9310): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 11:19:05.364: E/AndroidRuntime(9310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-22 11:19:05.364: E/AndroidRuntime(9310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-22 11:19:05.364: E/AndroidRuntime(9310): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试转换为活动并在AsyncTask中接收接口对象。
public MyAsyncHttpRequest(AsyncTaskCompleteListener act, SessionManager session ) {
try {
Log.i("TAG", "MyAsyncHttpRequest::Constructor");
this.activity = act;
this.callback = act; // Just here crashes all!!!
this.session = session;
} catch ( Exception exc ) {
exc.printStackTrace();
}
= new MyAsyncHttpRequest(AsyncTaskCompleteListener.class.cast(this), session);