问题
Following statement is not executing on some devices.
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
Is there any alternate method for above statement. On some devices it works fine but on other devices it gives exception.
Edit: My application is developed in tab. OnActivity results is used for getting picture from camera activity. Dont know where the resides but when I puts the above statement in try catch then no force close occurs and exception is shown there.
回答1:
The problem is not really with the casting, but the assumption that the Object returned by getLocalActivityManager().getCurrentActivity()
is of type AddEvent
. It would be difficult to say in what scenario does the getLocalActivityManager().getCurrentActivity()
return an Object that is of not the AddEvent type without looking at your implementation.
The following piece of code will check if the returned Object is indeed of type AddEvent
:
if(getLocalActivityManager().getCurrentActivity() instanceof AddEvent){
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
}
But in your case, it is recommended to check in what scenario does the getLocalActivityManager().getCurrentActivity()
return an instance of fable.eventippo.Home
as against fable.eventippo.AddEvent
.
回答2:
I am not sure where do you write this line but logcat output says: You are trying to cast Home
Activity into AddEvent
and this is why its giving you ClassCastException
.
FYI, getLocalActivityManager().getCurrentActivity();
is returning Home
.
来源:https://stackoverflow.com/questions/16601902/any-alternate-method-for-class-casting