对于类铸件任何替代方法 [英] Any Alternate method for Class Casting

查看:209
本文介绍了对于类铸件任何替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下列说法无法在某些设备上执行。

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.

编辑:我的应用程序在标签的发展。 OnActivity结果用于从相机获取的活动画面。不知道在哪里的所在,但是当我把上面的语句在尝试捕捉再没有出现强制关闭和异常显示在那​​里。

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.

推荐答案

这个问题是不是真的与铸造,但该对象由返回的假设getLocalActivityManager()。getCurrentActivity()的类型为的addEvent 。这将是很难做什么情况下说的 getLocalActivityManager()。getCurrentActivity()返回一个对象是不是的addEvent类型不看您的实现。

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.

以下code的一块将检查返回的对象确实是类型的的addEvent

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();
}

但在你的情况,建议在什么情况下不会在 getLocalActivityManager()。getCurrentActivity()返回寓言的实例来检查。 eventippo.Home 作为对 fable.eventippo.AddEvent

这篇关于对于类铸件任何替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆