Android 注释@UiThreadTest生成;java.lang.Exception:没有可运行的方法;

Android 注释@UiThreadTest生成;java.lang.Exception:没有可运行的方法;,android,unit-testing,junit4,Android,Unit Testing,Junit4,我一直在尝试编写一个与系统条一起工作的单元测试。我在尝试从错误的线程访问内容时出错: android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图 我已经读到,使用@UiThreadTest注释将导致我的测试用例在ui线程上运行并解决错误。但是,当我使用它时,我收到以下错误: java.lang.Exception:没有可运行的方法 我已经添加了UiThreadTestRule并确保导入了andro

我一直在尝试编写一个与系统条一起工作的单元测试。我在尝试从错误的线程访问内容时出错: android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图

我已经读到,使用@UiThreadTest注释将导致我的测试用例在ui线程上运行并解决错误。但是,当我使用它时,我收到以下错误: java.lang.Exception:没有可运行的方法

我已经添加了UiThreadTestRule并确保导入了android.support.test.annotation.UiThreadTest,但没有效果

我还缺什么吗

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import android.support.test.annotation.UiThreadTest;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)


/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class SystemBarSetterTest extends ActivityInstrumentationTestCase2<SystemBarSetter>
{

    private SystemBarSetter setter;
    public SystemBarSetterTest() {
        super(SystemBarSetter.class);
    }

    @Rule
    public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule();

    @Before
    public void setUp() throws Exception
    {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        setter = getActivity();
    }

    @Override
    public void runTestOnUiThread(Runnable r) throws Throwable {
        super.runTestOnUiThread(r);
    }

    @Test
    public void testImmersiveSticky() throws Exception
    {
        setter.setImmersiveStickyMode();
        int requiredFlags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        assertEquals(requiredFlags, setter.getCurrentUiVisibility()&requiredFlags);
    }
}
导入android.support.test.InstrumentationRegistry;
导入android.support.test.rule.UiThreadTestRule;
导入android.support.test.runner.AndroidJUnit4;
导入android.test.ActivityInstrumentationTestCase2;
导入android.view.view;
导入org.junit.Before;
导入org.junit.Rule;
导入org.junit.Test;
导入android.support.test.annotation.UiThreadTest;
导入org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
/**
* 
*/
公共类SystemBarSetterTest扩展了ActivityInstrumentationTestCase2
{
专用系统设置器;
公共系统barsettertest(){
super(SystemBarSetter.class);
}
@统治
public UiThreadTestRule mUiThreadTestRule=新UiThreadTestRule();
@以前
public void setUp()引发异常
{
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
setter=getActivity();
}
@凌驾
public void runTestOnUiThread(Runnable r)抛出可丢弃{
super.runTestOnUiThread(r);
}
@试验
public void testImmersiveSticky()引发异常
{
setter.setImmersiveStickyMode();
int requiredFlags=View.SYSTEM_UI_FLAG_全屏| View.SYSTEM_UI_FLAG_隐藏|导航| View.SYSTEM_UI_FLAG_沉浸式|;
assertEquals(requiredFlags、setter.getCurrentUiVisibility()&requiredFlags);
}
}

你最终明白了吗?