使用自定义操作栏布局时出现 Robolectric InflateException
Posted
技术标签:
【中文标题】使用自定义操作栏布局时出现 Robolectric InflateException【英文标题】:Robolectric InflateException when using custom action bar layout 【发布时间】:2014-12-06 00:32:31 【问题描述】:我刚刚通过这个setup 设置Robolectric
测试,当我测试我的应用程序时它给了我一个错误。
我已将问题缩小到ProgressSpinner
,它位于我的自定义ActionBar layout
中。当我禁用自定义 ActionBar 时,测试运行良好。我使用的是常规操作栏,而不是 ActionBarSherlock
。
有解决办法吗?
测试代码
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest
MainActivity activity;
@Before
public void setup()
this.activity = Robolectric.buildActivity(MainActivity.class).create().get();
@Test
public void shouldHaveHappySmiles() throws Exception
String hello = this.activity.getString(R.string.app_name);
assertThat(hello, equalTo("LFDate"));
故障跟踪
android.view.InflateException: XML file C:\Users\Deric\Documents\GitHub\lfdate\android\res\layout\actionbar.xml line #-1 (sorry, not yet implemented): Error inflating class android.widget.ProgressBar
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:312)
at android.support.v7.app.ActionBarImplICS.setCustomView(ActionBarImplICS.java:93)
at android.support.v7.app.ActionBarImplJB.setCustomView(ActionBarImplJB.java:20)
at com.walintukai.lfdate.MainActivity.onCreate(MainActivity.java:216)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at com.walintukai.lfdate.MainActivityTest.setup(MainActivityTest.java:20)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createView(LayoutInflater.java)
at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.android.internal.app.ActionBarImpl.$$robo$$ActionBarImpl_060b_setCustomView(ActionBarImpl.java:312)
at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java)
at android.support.v7.app.ActionBarImplICS.$$robo$$ActionBarImplICS_1498_setCustomView(ActionBarImplICS.java:93)
at android.support.v7.app.ActionBarImplICS.setCustomView(ActionBarImplICS.java)
at android.support.v7.app.ActionBarImplJB.setCustomView(ActionBarImplJB.java:20)
at com.walintukai.lfdate.MainActivity.onCreate(MainActivity.java:216)
at android.app.Activity.$$robo$$Activity_c57b_performCreate(Activity.java:5133)
at android.app.Activity.performCreate(Activity.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at com.walintukai.lfdate.MainActivityTest.setup(MainActivityTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
... 21 more
Caused by: java.lang.NullPointerException
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:165)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:55)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:494)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.content.Context.obtainStyledAttributes(Context.java:416)
at android.view.View.__constructor__(View.java:3317)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.shadows.ShadowView.__constructor__(ShadowView.java:63)
at android.view.View.<init>(View.java:3315)
at android.widget.ProgressBar.<init>(ProgressBar.java:253)
at android.widget.ProgressBar.<init>(ProgressBar.java:246)
at android.widget.ProgressBar.<init>(ProgressBar.java:242)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:312)
at android.support.v7.app.ActionBarImplICS.setCustomView(ActionBarImplICS.java:93)
at android.support.v7.app.ActionBarImplJB.setCustomView(ActionBarImplJB.java:20)
at com.walintukai.lfdate.MainActivity.onCreate(MainActivity.java:216)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at com.walintukai.lfdate.MainActivityTest.setup(MainActivityTest.java:20)
... 21 more
【问题讨论】:
发布您的 build.gradle。什么版本的 robolectric? 【参考方案1】:创建或添加 project.properties 文件到你的应用目录(如果有不同的模块,则不是测试目录)
将这两行添加到新创建的文件中
android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3
android.library.reference.2=../../build/intermediates/exploded-aar/com.android.support/support-v4/21.0.3
路径应该指明这些引用的清单文件
【讨论】:
以上是关于使用自定义操作栏布局时出现 Robolectric InflateException的主要内容,如果未能解决你的问题,请参考以下文章
调用自定义 Button 类的构造函数时出现 ClassCastException
QListWidget自定义item时出现布局显示误差解决方法
使用 WiX C#/.NET 4 自定义操作时出现错误 2896