扩展的Application类上的Android Library Project转换错误

时间:2014-02-21 21:09:34

标签: android

我正在使用我的应用程序。要做到这一点,我已经:

  • 将我的Android应用程序项目转换为Android库项目。我们称之为com.mylibraryproject.app
  • 创建了一个新项目。我们称之为com.example.testproject
  • 将我的Android Library Project添加为我的新Android应用程序项目的库。
  • 将我的Android图书馆计划中的清单复制到我的新Android应用程序项目的清单中,并适当地引用了这些活动。

示例:

<activity
      android:name="com.mylibraryproject.app.activity.MyActivity"
      android:screenOrientation="portrait" >
</activity>

但是,当我的新Android应用程序项目运行时,我遇到了崩溃。

库项目包含一个类,我们称之为MyApp,它扩展了Application。在整个项目中,都有对(MyApp)getApplicationContext()的引用。

当命中上面一行时,抛出以下异常。我该如何避免这种情况?

更新

以下是完整的onResume()方法和日志cat:

@Override
protected void onResume() {
    super.onResume();
    MyApp app = (MyApp)getApplication();
    if (app.getUserId() == -1 && !app.getUserConnected() && app.loadLastUser()) {
        updateDisplay();
    } else if (!mBack && app.getUserConnected()) {
        updateDisplay();
    }
}

这是例外

02-21 13:13:11.169: E/AndroidRuntime(469): FATAL EXCEPTION: main
02-21 13:13:11.169: E/AndroidRuntime(469): java.lang.RuntimeException: Unable to resume activity {com.example.testproject/com.mylibraryproject.app.activity.MyActivity}: java.lang.ClassCastException: android.app.Application
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.os.Looper.loop(Looper.java:130)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 13:13:11.169: E/AndroidRuntime(469):  at java.lang.reflect.Method.invokeNative(Native Method)
02-21 13:13:11.169: E/AndroidRuntime(469):  at java.lang.reflect.Method.invoke(Method.java:507)
02-21 13:13:11.169: E/AndroidRuntime(469):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-21 13:13:11.169: E/AndroidRuntime(469):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-21 13:13:11.169: E/AndroidRuntime(469):  at dalvik.system.NativeStart.main(Native Method)
02-21 13:13:11.169: E/AndroidRuntime(469): Caused by: java.lang.ClassCastException: android.app.Application
02-21 13:13:11.169: E/AndroidRuntime(469):  at com.mylibraryproject.app.activity.MyActivity.onResume(MyActivity.java:277)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.Activity.performResume(Activity.java:3832)
02-21 13:13:11.169: E/AndroidRuntime(469):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
02-21 13:13:11.169: E/AndroidRuntime(469):  ... 12 more

第277行是这一行:

MyApp app = (MyApp)getApplication();

1 个答案:

答案 0 :(得分:2)

ApplicationApplicationContext不是一回事。您需要转换getApplication()结果

当然,施法只是说“我知道这个对象是一个xyz所以让我把它视为那个” ......它实际上并没有将一个对象转换成另一个对象

修改

更改您的onResume,如下所示

@Override
protected void onResume() {
    super.onResume();
    MyApp app = (MyApp)getApplication();
    if (app.getUserId() == -1 && !app.getUserConnected() && app.loadLastUser()) {
        updateDisplay();
    } else if (!mBack && app.getUserConnected()) {
        updateDisplay();
    }
}

<application android:name="com.mypackage.MyApp"
    ....>