我有一个活动,直接加载一个字符串数组
private String[] questions = {"Q1", "Q2", "Q3"}; private String[] answers= {"A1", "A2", "A3"};我想将其更改为从资源文件加载
资源文件名为Questions
<string-array name="questions"> <item>Q1</item> <item>Q2</item> <item>Q3</item> </string-array> <string-array name="answers"> <item>A1</item> <item>A2</item> <item>A3</item> </string-array>将字符串数组更改为
private String[] questions = getResources().getStringArray(R.array.questions); private String[] answers= getResources().getStringArray(R.array.answers);当我更改它以从我的资源文件中提取字符串数组时,它给了我
java.lang.IllegalStateException: My_Fragment{38ad4cd} not attached to ActivityI have an activity that loaded a string array directly into it
private String[] questions = {"Q1", "Q2", "Q3"}; private String[] answers= {"A1", "A2", "A3"};I wanted to change it to load from a resource file
Resource File named Questions
<string-array name="questions"> <item>Q1</item> <item>Q2</item> <item>Q3</item> </string-array> <string-array name="answers"> <item>A1</item> <item>A2</item> <item>A3</item> </string-array>Changed the string array to
private String[] questions = getResources().getStringArray(R.array.questions); private String[] answers= getResources().getStringArray(R.array.answers);When I changed it to pull the string array from my resource file it gives me
java.lang.IllegalStateException: My_Fragment{38ad4cd} not attached to Activity最满意答案
这是怎么回事?
您的实际代码所做的是它在实例化Fragment时尝试访问资源,这是错误的,因为除非您的Fragment附加到某些Activity否则资源将不可用。
另外: getResources()只是getActivity().getResources()的快捷方式。 我的猜测是它的实现看起来像这样:
if(isAttached()) { return getActivity().getResources(); } else { throw new RuntimeException("Fragment is not attached..."); }解
移动此初始化:
private String[] questions = getResources().getStringArray(R.array.questions); private String[] answers = getResources().getStringArray(R.array.answers);到你的onCreateView方法:
private String[] questions; private String[] answers; public void onCreateView(...){ // ... questions = getResources().getStringArray(R.array.questions); answers = getResources().getStringArray(R.array.answers); // ... }注意:您可以安全地在onAttach和onDetach回调之间的任何位置移动此初始化。 检查片段生命周期以获得清晰的想法。
类似的问题,但有一个活动
从字符串资源获取时应用程序崩溃 。
What's going on?
What your actual code does is that it tries to access the resources when the Fragment is instantiated, which is wrong because the resources won't be available unless your Fragment is attached to some Activity.
Plus
The call getResources() is just a shortcut to getActivity().getResources(). My guess is that its implementation looks like this:
if(isAttached()) { return getActivity().getResources(); } else { throw new RuntimeException("Fragment is not attached..."); }Solution
Move this initialization:
private String[] questions = getResources().getStringArray(R.array.questions); private String[] answers = getResources().getStringArray(R.array.answers);To your onCreateView method:
private String[] questions; private String[] answers; public void onCreateView(...){ // ... questions = getResources().getStringArray(R.array.questions); answers = getResources().getStringArray(R.array.answers); // ... }Note
You're safe to move this initialization anywhere in between onAttach and onDetach callbacks. Check the fragment life-cycle to get a clear idea.
Similar question but with an activity
App crashing when fetching from string ressources.
更多推荐
发布评论