任何类别铸造的替代方法 [英] Any Alternate method for Class Casting

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

问题描述

以下语句在某些设备上未执行。

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结果用于从相机活动中获取照片。不知道所在的位置,但是当我将上面的语句放在try catch中时,没有强制关闭,并且在那里显示异常。

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类型的Object。

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.

以下代码将检查返回的对象确实是类型 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();
}

但在您的情况下,建议您查看哪些场景code> getLocalActivityManager()。getCurrentActivity()返回一个 fable.eventippo.Home 的实例与 fable .eventippo.AddEvent

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.

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

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