Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any Alternate method for Class Casting

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.

Click here... Yellow text shows 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.

like image 682
Varun Vishnoi Avatar asked May 17 '13 05:05

Varun Vishnoi


Video Answer


2 Answers

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.

like image 172
Rajesh Avatar answered Sep 27 '22 21:09

Rajesh


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.

like image 32
Paresh Mayani Avatar answered Sep 27 '22 21:09

Paresh Mayani