Unable to execute isDisplayed() method

Hi , I am very new to Appium and I am stuck at a very basic thing which i am unable to resolve.

Actually i wanted to launch an other application when one of my code block was executed , but it was not possible.
So I tried this :
public void LaunchYT() {

dr.sendKeyEvent(3);
dr.findElementByAccessibilityId(“Apps”).click();

if(dr.findElementByName(“YouTube”).isDisplayed()){

dr.swipe(664, 690, 54, 690, 0);

}else dr.findElementByName(“YouTube”).click() ;

The problem is that ,once it enters if condition , it starts searching for the element and the time out occurs and it doesn’t go to next step at all.

Please suggest

You can launch another app at any time in appium using

driver.startActivity(“appPacakge”,“appActivity”); // pass package and activity of another app make sure app should be installed

It might be that statement “dr.findElementByName(“YouTube”)” returns 0 elements. Try below statement

dr.findElementByName(“YouTube”).size()>0

Thanks a Lot friend.It worked.