无法在未调用Looper.prepare()new的线程内创建处理程序

时间:2017-05-02 18:50:00

标签: android

我目前正在尝试运行带有屏幕截图的esspresso测试。我的编码实验是非常基础的,如果有人有一个非常棒的简单解决方案。 当我尝试运行此测试时,我得到以下Stack Trace:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Activity.<init>(Activity.java:794)
at com.lifehealth.irma.test.Screenshotscripts.Screencap.<init>(Screencap.java:70)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

以下是我的代码:

import android.app.Activity;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import com.lifehealth.MainActivity;
import com.lifehealth.irma.R;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.doubleClick;
import static android.support.test.espresso.action.ViewActions.swipeUp;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
@LargeTest

// Name of the activity + Test
public class Screencap extends Activity {
    //Strings to be typed into tests declaration
    public static final String STRING_TO_BE_TYPED = "Test1233";
    public static final String loginID = "LIFEHEALTH";
    public static final String Device = "TestDevice";


    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    //@Test must be there to start a test if this is missing it will not run the test
    @Test
    public void Screencap1 () { /* passes*/
        //Enters login
        onView(withId(R.id.editOid)) /*editTextUserInput*/
                .perform(typeText(loginID), closeSoftKeyboard());
        // then click next
        onView(withId(R.id.btn_next)).perform(click());
        // "Clicks the settings
        onView(withId(R.id.btnSettings)).perform(click());
        //Clicks Device Settings.
        onView(withText("Device Settings")).perform(click());
        //Scroll up
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Configure IRMA Base")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        onView(withText("Barcode Reader Timeout")).perform(swipeUp());
        //language
        onView(withText("Language")).perform(doubleClick());
        HelperClass.takeScreenshot("Whatever you want to call the file", Screencap.this);

        //wait 1 min
        try {
            Thread.sleep(1 * 60 * 1000);
        } catch (InterruptedException e) {

        }
        /* Clicks back button */
        onView(withId(R.id.btn_back)).perform(click());
        //clicks main menu button
        onView(withText("Main Menu")).perform(click());

    }



}

1 个答案:

答案 0 :(得分:0)

您不应该让您的测试类 Screencap 扩展活动

在类 Activity 的构造函数中,它尝试在未调用Looper.prepare()的线程内创建处理程序。