Android Library Project casting error on extended Application class
I am whitelabeling my app. To do this, I've:
com.mylibraryproject.app
. com.example.testproject
. Example:
<activity
android:name="com.mylibraryproject.app.activity.MyActivity"
android:screenOrientation="portrait" >
</activity>
However, I'm having a crash when my new Android Application Project runs.
The library project contains a class, we'll call it MyApp
, that extends Application
. All over this project, there are references to (MyApp)getApplicationContext()
.
When the above line is hit, the below exception is thrown. How do I avoid this?
Update:
Here is the full onResume()
method and log 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();
}
}
Here is the exception
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
Line 277 is this line:
MyApp app = (MyApp)getApplication();
Application
an ApplicationContext
aren't the same thing. You need to cast a getApplication()
result instead
Of course, casting just says that "I know this object is an xyz so let me treat it as that" ... it doesn't actually transmute one object into another type
Edit:
Change your onResume like below
@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/66178.html