机器人内部的NPE

时间:2014-05-07 13:36:09

标签: android testing integration-testing robotium

我必须进行一些不稳定的测试,并且遇到一个例外,我有时会使用robotium

Caused by: java.lang.NullPointerException
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:44)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at com.jayway.android.robotium.solo.ViewFetcher.getTopParent(ViewFetcher.java:46)
at com.jayway.android.robotium.solo.PublicViewFetcher.getTopParent(PublicViewFetcher.java:6)
at query.Query.rootViews(Query.java:113)
at query.Query.executeQuery(Query.java:29)

这是来自robotium的getTopParent方法:

/**
 * Returns the absolute top parent {@code View} in for a given {@code View}.
 *
 * @param view the {@code View} whose top parent is requested
 * @return the top parent {@code View}
 */

public View getTopParent(View view) {
    if (view.getParent() != null // line 44
            && view.getParent() instanceof android.view.View) {
        return getTopParent((View) view.getParent()); // line 46
    } else {
        return view;
    }
}

据我所知,这意味着view.getParent()在null-check和从视图的递归调用之间发生变化? 我可以想象向机器人发出拉动请求,将getTopParent更改为:

/**
 * Returns the absolute top parent {@code View} in for a given {@code View}.
 *
 * @param view the {@code View} whose top parent is requested
 * @return the top parent {@code View}
 */

public View getTopParent(View view) {
            final ViewParent parent = view.getParent();
    if (parent != null && parent instanceof android.view.View) {
        return getTopParent((View) parent);
    } else {
        return view;
    }
}

但我希望在此之前有更多意见。