报错:由getResources()引起的:Fragment MyFragment not attached to Activity
来源:互联网 发布:Ubuntu纯命令行打中文 编辑:程序博客网 时间:2024/06/11 17:47
出错误的原因是因为我在某一Fragment的开始,使用了变量:
private String[] modeTexts=new String[]{getResources().getString(R.string.modetext_scan),getResources().getString(R.string.modetext_search)};
然后提示在MainActivity中不能够加载该Fragment,问题归结为在非Activity里面如何调用getresources文件。解决方法如下:
初始化:
private int[] modeTexts=new int[]{R.string.modetext_voice,R.string.modetext_scan,R.string.modetext_search};
使用的时候 ,因为应该是已经初始化之后才使用的,所以:
modeTextView.setText(getResources().getString(modeTexts[modeIndex])); 就可以啦
补充:
获取string.xml文件里面的值有几个不同的地方。
1.在AndroidManifest.xml与layout等xml文件里:
android:text="@string/resource_name"
2.在activity里:
方法一:this.getString(R.string.resource_name);
方法二:getResources().getString(R.string.resource_name);
3.在其他java文件(必须有Context或pplication)
方法一: context.getString(R.string.resource_name);
方法二: application.getString(R.string.resource_name);
- 报错:由getResources()引起的:Fragment MyFragment not attached to Activity
- Fragment MyFragment not attached to Activity
- Fragment MyFragment not attached to Activity
- Android bug Fragment MyFragment not attached to Activity
- Fragment not attached to Activity
- Fragment not attached to Activity
- Fragment not attached to Activity
- Fragment not attached to Activity
- Fragment not attached to Activity
- fragment not attached to Activity
- Fragment not attached to Activity 异常
- 关于Fragment not attached to Activity 异常
- Android - Fragment StepCounterStaticFragment not attached to Activity
- Android IllegalStateException: Fragment not attached to Activity
- Fragment not attached to Activity 异常
- Fragment not attached to Activity 异常
- Fragment not attached to Activity 异常
- Fragment not attached to activity 异常
- UIButton与NSTimer之间的奇葩bug
- Qt5.0调用GDAL库函数
- 1222 -- 炸金花
- SimpleJdbc: 用于简化数据库操作的Java库
- JS 获取HTML之间的传输的参数的值.
- 报错:由getResources()引起的:Fragment MyFragment not attached to Activity
- Android开发艺术探索——互动出版网
- 1223 -- 德玛西亚正义
- 第二周项目0-宣告“主权”
- 第2周项目0-宣告“主权”
- Apache与Tomcat 区别联系
- net::ERR_CONNECTION_RESET 连接已重置
- 1224 -- 植物大战僵尸
- Java设计模式 之 命令模式