扩展应用程序类上的Android库项目转换错误
我正在贴上我的应用程序。 要做到这一点,我已经:
com.mylibraryproject.app
。 com.example.testproject
。 例:
<activity
android:name="com.mylibraryproject.app.activity.MyActivity"
android:screenOrientation="portrait" >
</activity>
但是,当我的新Android应用程序项目运行时,我正在崩溃。
库项目包含一个类,我们称之为MyApp
,它扩展了Application
。 在这个项目中,有(MyApp)getApplicationContext()
。
当上面的命中时,下面的异常被抛出。 我如何避免这种情况?
更新:
这里是完整的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();
}
}
这是例外
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();
Application
的ApplicationContext
不是一回事。 您需要转换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"
....>
链接地址: http://www.djcxy.com/p/66177.html
上一篇: Android Library Project casting error on extended Application class
下一篇: ACRA is crashing