ChrisZou/android-unit-testing-tutorial

使用Robolectric单元测试一直报错,应该怎么解决

YanYanLun opened this issue · 4 comments

android.view.InflateException: XML file build\intermediates\res\merged\debug\layout\activity_login.xml line #-1 (sorry, not yet implemented): Error inflating class EditText

at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.sparxo.checkin.activity.LoginActivity.onCreate(LoginActivity.java:52)
at android.app.Activity.performCreate(Activity.java:5933)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at com.sparxo.checkin.LoginActivityTest.testView(LoginActivityTest.java:22)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Caused by: java.lang.NumberFormatException: For input string: "@0"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:128)
at android.content.res.TypedArray.getInt(TypedArray.java:322)
at android.widget.TextView.constructor(TextView.java:1046)
at android.widget.TextView.(TextView.java)
at android.widget.EditText.(EditText.java)
at android.support.v7.widget.AppCompatEditText.(AppCompatEditText.java)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:108)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
... 41 more

Process finished with exit code -1

@RunWith(RobolectricGradleTestRunner.class)
@config(constants = BuildConfig.class, sdk = 21)
public class LoginActivityTest {
@test
public void testView() {
LoginActivity activity = Robolectric.setupActivity(LoginActivity.class);
EditText email = (EditText) activity.findViewById(R.id.email);
Assert.assertNotNull(email);
}
}

这个是你自己写的项目吗?还是这个 项目?

这个是我在看了你写的项目下,自己写的,但是在sdk 23下面运行就会出错,我项目中有使用v4和v7包,我不知道怎么去解决这个问题,所以前来请教你

看你的Error log
Caused by: java.lang.NumberFormatException: For input string: "@0"
感觉是XML里面哪个地方值填错了。

的确如你所说,xml里面有个属性设置错误导致,已经解决,谢谢你的帮助