Any Alternate method for Class Casting

本秂侑毒 提交于 2020-01-03 15:22:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!