2014-09-22 169 views
0

你好,我已經實現了MyAsyncHttpRequest任務,AsyncTask如下ClassCastException異常麻煩

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

而且

這樣

public interface AsyncTaskCompleteListener {  
    public void onTaskComplete(ArrayList list, int cmd); 
} 

和活動,我稱之爲MyAsyncHttpRequest公共接口AsyncTaskCompleteListener像這樣

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

你嘗試過'this.callback = act;'? – 2014-09-22 10:02:11

+0

它得到一個編譯錯誤類型不匹配:不能從活動轉換爲AsyncTaskCompleteListener' – 2014-09-22 10:07:16

+3

我敢肯定,你有錯誤的'進口'和AsyncTaskCompleteListener從這一行'公共類WmsMapActivity擴展活動實現AsyncTaskCompleteListener'是不一樣的這行'this.callback =(AsyncTaskCompleteListener)act;' – Selvin 2014-09-22 10:09:43

回答

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); 
+0

我也一樣。 – 2014-09-22 10:46:37

+0

hm ..看起來你沒有實現接口。也許這是IDE錯誤,當最後的代碼不適用於構建。嘗試重建項目並清除緩存。你用什麼IDE? – smail2133 2014-09-22 11:07:53

+0

@ smail123是的,我做了,請看看'公共類WmsMapActivity擴展活動實現AsyncTaskCompleteListener {...'和'onCompleteTask'實現。問候 – 2014-09-22 11:52:03