使用资源文件时,片段未附加到活动(Fragment not attached to activity when using resource file)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
使用资源文件时,片段未附加到活动(Fragment not attached to activity when using resource file)

我有一个活动,直接加载一个字符串数组

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 Activity

I 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.

更多推荐

本文发布于:2023-04-18 00:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d3984c6d991ab705df3542369d0a37d0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   文件   资源   Fragment   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!