Web服务调用期间的致命异常

时间:2014-06-13 18:38:42

标签: java android fatal-error classcastexception forceclose

我在执行网络服务电话时遇到致命异常。我最近实现了一个回调(在一个effor中删除之前实现的get()调用)但是我似乎忽略了一些事情,因为在调试时抛出一个异常并且应用程序崩溃了一个致命的异常。

问题源于以下几行:

appIdResponse =(ActionCompletedListener)new GetappIdTask(                             variables.login,variables.MAC,listener).execute();

但是,我不确定如何修改该行来解决问题并避免ClassCastException。

来源:

ActionCompletedListener appIdResponse = null;

                try {

                    ActionCompletedListener listener = new ActionCompletedListener() {

                        @Override
                        public void callback(Object o) {
                            // O is your response, cast to the type you need
                            GetappIdResponse appIdResponse = (GetappIdResponse) o;
                            // Handle response

                            if (appIdResponse != null) {
                                variables.login.entityID = ((GetappIdResponse) appIdResponse)
                                        .getappID();

                            } else {
                                Toast toast = Toast
                                        .makeText(
                                                getApplication(),
                                                "Could not properly retrieve app data. Please check your network connection",
                                                Toast.LENGTH_LONG);
                                toast.show();
                                AlertDialog.Builder builder = new AlertDialog.Builder(
                                        ActivityMain.this);
                                builder.setMessage(
                                        getResources().getString(R.string.app_id_failure))
                                        .setCancelable(false)
                                        .setPositiveButton("OK",
                                                new DialogInterface.OnClickListener() {
                                            public void onClick(
                                                    DialogInterface dialog, int id) {

                                            }
                                        });
                                AlertDialog alert = builder.create();
                                alert.show();
    //                          return false;

                            }


                        }
                    };

                    appIdResponse = (ActionCompletedListener) new GetappIdTask(
                            variables.login, variables.MAC, listener).execute();

logcat的:

06-12 12:21:12.180: W/System.err(3294): java.lang.ClassCastException: com.example.login.wstasks.GetappIdTask cannot be cast to com.example.common.utils.ActionCompletedListener
06-12 12:21:12.180: D/dalvikvm(3294): GC_CONCURRENT freed 478K, 10% free 4666K/5184K, paused 1ms+3ms, total 15ms
06-12 12:21:12.180: W/System.err(3294):     at com.example.app.ActivityMain.getapp(ActivityMain.java:713)
06-12 12:21:12.180: W/System.err(3294):     at com.example.app.ActivityMain.initializeVariables(ActivityMain.java:928)
06-12 12:21:12.180: W/System.err(3294):     at com.example.app.ActivityMain.onCreate(ActivityMain.java:161)
06-12 12:21:12.180: W/System.err(3294):     at android.app.Activity.performCreate(Activity.java:5104)
06-12 12:21:12.180: W/System.err(3294):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-12 12:21:12.180: W/System.err(3294):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-12 12:21:12.180: W/System.err(3294):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-12 12:21:12.180: W/System.err(3294):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-12 12:21:12.180: W/System.err(3294):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-12 12:21:12.180: W/System.err(3294):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 12:21:12.180: W/System.err(3294):     at android.os.Looper.loop(Looper.java:137)
06-12 12:21:12.180: W/System.err(3294):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-12 12:21:12.180: W/System.err(3294):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 12:21:12.180: W/System.err(3294):     at java.lang.reflect.Method.invoke(Method.java:511)
06-12 12:21:12.180: W/System.err(3294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-12 12:21:12.180: W/System.err(3294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-12 12:21:12.180: W/System.err(3294):     at dalvik.system.NativeStart.main(Native Method)
06-12 12:21:12.200: V/TaskManager(3294): In initializeQueue() on MAIN THREAD
06-12 12:21:12.200: D/AndroidRuntime(3294): Shutting down VM
06-12 12:21:12.200: W/dalvikvm(3294): threadid=1: thread exiting with uncaught exception (group=0x41c93930)
06-12 12:21:12.200: E/AndroidRuntime(3294): FATAL EXCEPTION: main
06-12 12:21:12.200: E/AndroidRuntime(3294): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.ActivityMain}: java.lang.NullPointerException
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.os.Looper.loop(Looper.java:137)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at java.lang.reflect.Method.invoke(Method.java:511)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at dalvik.system.NativeStart.main(Native Method)
06-12 12:21:12.200: E/AndroidRuntime(3294): Caused by: java.lang.NullPointerException
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.taskmanager.tasks.TaskFactory.getTaskByName(TaskFactory.java:51)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.taskmanager.TaskManager.initializeQueue(TaskManager.java:92)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.taskmanager.TaskManager.<init>(TaskManager.java:71)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.ActivityMain.keepAlive(ActivityMain.java:479)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.ActivityMain.initializeVariables(ActivityMain.java:947)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at com.example.app.ActivityMain.onCreate(ActivityMain.java:161)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.Activity.performCreate(Activity.java:5104)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-12 12:21:12.200: E/AndroidRuntime(3294):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-12 12:21:12.200: E/AndroidRuntime(3294):     ... 11 more
06-12 12:21:12.210: W/System(3294): Ignoring HTTP header field 'WS_AUTH_TOKEN' because its value is null

1 个答案:

答案 0 :(得分:0)

要避免使用ClassCastException,请不要将某些内容转换为错误的类。从问题行中删除appIdResponse = (ActionCompletedListener)应该可以帮助您摆脱该异常。但看起来您的代码可能还存在其他问题。