gpt4 book ai didi

android - 使用Robolectric测试ListPreference时出现RuntimeException

转载 作者:行者123 更新时间:2023-11-29 01:32:18 25 4
gpt4 key购买 nike

我正在尝试使用Robolectric 3.0-RC3对PreferenceFragment进行单元测试。我的首选项在preferences.xml文件中定义,有几个ListPreference

当我开始使用FragmentTestUtil.startFragment(fragment)测试片段时,在addPreferencesFromResource(R.xml.preferences)onCreate方法中调用方法PreferenceFragment

我得到以下异常:

原因:java.lang.RuntimeException:是吗? ResName {com.myapp:array / pageOrientation_entries}
    在org.robolectric.shadows.Converter.convertAndFill(Converter.java:99)
    在org.robolectric.shadows.Converter.convertAndFill(Converter.java:50)
    在org.robolectric.shadows.ShadowResources.createTypedArray(ShadowResources.java:232)
    在org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:209)
    在org.robolectric.shadows.ShadowResources.access $ 000(ShadowResources.java:55)
    在org.robolectric.shadows.ShadowResources $ ShadowTheme.obtainStyledAttributes(ShadowResources.java:485)
    在android.content.res.Resources $ Theme.obtainStyledAttributes(Resources.java)
    在android.content.Context.obtainStyledAttributes(Context.java:472)
    在android.preference.ListPreference.constructor(ListPreference.java:49)
    在android.preference.ListPreference。(ListPreference.java)
    在android.preference.GenericInflater.createItem(GenericInflater.java:383)
    在android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
    在android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
    在android.preference.GenericInflater.rInflate(GenericInflater.java:481)
    在android.preference.GenericInflater.rInflate(GenericInflater.java:493)
    在android.preference.GenericInflater.inflate(GenericInflater.java:326)
    在android.preference.GenericInflater.inflate(GenericInflater.java:263)
    在android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
    在android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:299)
    在com.myapp.ui.settings.SettingsFragment.onCreate(SettingsFragment.java:53)
    在android.app.Fragment.performCreate(Fragment.java:2031)
    在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
    在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    在android.app.BackStackRecord.run(BackStackRecord.java:833)
    在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
    在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447)
    在android.os.Handler.handleCallback(Handler.java:739)
    在android.os.Handler.dispatchMessage(Handler.java:95)
    在org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
    在org.robolectric.shadows.ShadowMessageQueue.access $ 100(ShadowMessageQueue.java:29)
    在org.robolectric.shadows.ShadowMessageQueue $ 1.run(ShadowMessageQueue.java:95)
    在org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
    在org.robolectric.util.Scheduler.postAtFrontOfQueue(Scheduler.java:98)
    在org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:114)
    在android.os.MessageQueue.enqueueMessage(MessageQueue.java)
    在android.os.Handler.enqueueMessage(Handler.java:631)
    在android.os.Handler.sendMessageAtTime(Handler.java:600)
    在android.os.Handler.sendMessageDelayed(Handler.java:570)
    在android.os.Handler.post(Handler.java:326)
    在android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
    在android.app.BackStackRecord.commitInternal(BackStackRecord.java:728)
    在android.app.BackStackRecord.commit(BackStackRecord.java:704)
    在org.robolectric.util.FragmentTestUtil.startFragment(FragmentTestUtil.java:14)
    在com.myapp.ui.settings.SettingsFragmentTest.startFragment(SettingsFragmentTest.java:40)
    在com.myapp.ui.settings.SettingsFragmentTest.plusFeaturesDisabled_onSignaturePrefClick(SettingsFragmentTest.java:70)
    ...另外18个

当我注释掉所有ListPreference并保留其他种类的首选项时,我没有这个问题。

谢谢你的帮助。

最佳答案

似乎Robolectric 3.0在处理通用数组资源方面存在问题,例如:

<array>
<item>Option 1</item>
</array>


就我而言,我可以通过将其显式更改为正确的数组类型来解决此问题:

<string-array>
<item>Option 1</item>
</string-array>

关于android - 使用Robolectric测试ListPreference时出现RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com