UIAutomator控件未找到时不退出,继续运行

      UiCollection ScrollView_collections = new UiCollection(new UiSelector().className("android.widget.ScrollView"));

      UiSelector childPattern = new UiSelector().className("android.widget.TextView");

      UiObject tmp2=ScrollView_collections.getChildByText(childPattern, "去完成")

      ...

      上面代码在"去完成"不存在,tmp2控件未找到时的,会弹出异常,然后程序直接终止。

      但希望的逻辑是如果不存在就略过,流程还需要继续走下去。通过try捕获异常的同时,加空指针判断,就能够避免程序退出,最后的代码是这样:

      UiCollection ScrollView_collections = new UiCollection(new UiSelector().className("android.widget.ScrollView"));

      UiSelector childPattern = new UiSelector().className("android.widget.TextView");

      UiObject tmp2=null;

      try{

           tmp2=ScrollView_collections.getChildByText(childPattern, "去完成");

           if(tmp2!=null &&tmp2.exists()){

                   tmp2.click();

            }

       }catch(UiObjectNotFoundException e){

       }

你可能感兴趣的:(UIAutomator控件未找到时不退出,继续运行)