0

我是新来的,我刚刚学会了如何编写 Android 应用程序。

现在,我想用 Droid Fu 库制作一个应用程序(这是一个很好的库,但缺乏文档和示例)。我的 handleApplicationClosing 函数遇到问题。当我在我的应用程序上按下后退按钮时会出现问题,这意味着应用程序正在关闭(因为我只尝试使用 droid fu 测试应用程序的 1 个活动)。

经过多次尝试调试程序,我发现当我的代码到达这个函数时发生了错误:

static void handleApplicationClosing(final Context context, int keyCode) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(2);

        RunningTaskInfo currentTask = tasks.get(0);
        RunningTaskInfo nextTask = tasks.get(1);

        // if we're looking at this application's base/launcher Activity,
        // and the next task is the Android home screen, then we know we're
        // about to close the app
        if (currentTask.topActivity.equals(currentTask.baseActivity)
                && nextTask.baseActivity.getPackageName().startsWith("com.android.launcher")) {
            DroidFuApplication application = (DroidFuApplication) context
                    .getApplicationContext();
            application.onClose();
        }
    }
}

在这条线上:

 DroidFuApplication application = (DroidFuApplication) context
                    .getApplicationContext();

错误代码是:

java.lang 类 ClassCastException

这是什么意思?

4

1 回答 1

1

我从未使用过DroidFu,但请确保您已添加下一行AndrodiManifest.xml

<manifest ...>
   ...
   <application .... android:name="com.github.droidfu.DroidFuApplication">
      ...
   </application>
   ...
</manifest>
于 2011-12-14T07:54:32.520 回答