Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建新的SpannableStringBuilder将返回null_Java_Android_Spannablestringbuilder - Fatal编程技术网

Java 创建新的SpannableStringBuilder将返回null

Java 创建新的SpannableStringBuilder将返回null,java,android,spannablestringbuilder,Java,Android,Spannablestringbuilder,这是我的单元测试。不知何故,当我使用非null字符串参数创建实例化SpannableStringBuilder实例时,它似乎为null。这个代码怎么了 public class CardNumberTextWatcherTest { private CardNumberTextWatcher watcher; @Before public void setUp() throws Exception { watcher = new CardNumberTextWatcher(); }

这是我的单元测试。不知何故,当我使用非null字符串参数创建实例化SpannableStringBuilder实例时,它似乎为null。这个代码怎么了

public class CardNumberTextWatcherTest {

private CardNumberTextWatcher watcher;

@Before
public void setUp() throws Exception {
    watcher = new CardNumberTextWatcher();
}


@Test
public void afterTextChanged_cardNumberDividedWithSpaces() {
    assertTransformText("1111222233334444", "1111 2222 3333 4444");
}

private void assertTransformText(final String testStr,
                                 final String expectedStr) {
    final CardNumberTextWatcher watcher = new CardNumberTextWatcher();
    final Editable actualStr = new SpannableStringBuilder(testStr);
    // When I debug I see that actualStr value is null. Therefore test 
    //doesn't pass. What's the problem?
    watcher.transformText(actualStr);
    assertEquals(expectedStr, actualStr.toString());
}


}

您正在调用的是断言所比较的
actualStr.toString()
,而不是
actualStr
本身,因此请通过
SpannableStringBuilder
查找路由,以查看从
toString()
可能导致
null
的原因。可能
testStr
null

问题出在测试目录中。只要
SpannableStringBuilder
是Android SDK的一部分,它就无法在常规JUnit测试目录中解析。当我将我的类移动到androidTest目录时,一切都开始按预期工作

@RunWith(RobolectricTestRunner::class)

使用Robolectric解决了这个问题。

您能提供堆栈跟踪或测试输出吗?请共享您的异常堆栈跟踪,以便我们可以帮助您解决同样的问题。其实我不知道,为什么这个问题这么少。