Can't make static reference to non-static method ( Android getApplicationContext() )
2016-07-28 11:20
489 查看
The reason you were given for the error is correct. The fragments are static inner classes, they are in a scope
in which an instance of the Activity does not exist. You then have a method which you call which can only be referenced from an instance of the Activity. Since you don't have an instance, you can't make that call. For this situation, there are two basic fixes.
1) Remove the static
keyword from the Fragment class definition. This would tie the definition of the class, and the instances of the class, to an instance of the the Activity and you would gain access to the Activity's instance methods.
2) Call getActivity().getApplicationContext() instead.
The method getActivity() comes from the Fragment class and gives you access to the methods of the Activity. If you need a specific method that you added to the Activity, then you would need to cast:((MyActivity)getActivity()).myMethod();
The second approach is probably the better one. The Fragment lifecycle is independent of the Activity lifecycle,
and by making the Fragment an instance-level inner class you would end up tieing the two together. The FragmentManager may end up not liking it, and there could be issues when you do things like rotatge the screen.
https://coderanch.com/t/632507/Android/Mobile/Error-fix-static-reference-static
in which an instance of the Activity does not exist. You then have a method which you call which can only be referenced from an instance of the Activity. Since you don't have an instance, you can't make that call. For this situation, there are two basic fixes.
1) Remove the static
keyword from the Fragment class definition. This would tie the definition of the class, and the instances of the class, to an instance of the the Activity and you would gain access to the Activity's instance methods.
2) Call getActivity().getApplicationContext() instead.
The method getActivity() comes from the Fragment class and gives you access to the methods of the Activity. If you need a specific method that you added to the Activity, then you would need to cast:((MyActivity)getActivity()).myMethod();
The second approach is probably the better one. The Fragment lifecycle is independent of the Activity lifecycle,
and by making the Fragment an instance-level inner class you would end up tieing the two together. The FragmentManager may end up not liking it, and there could be issues when you do things like rotatge the screen.
https://coderanch.com/t/632507/Android/Mobile/Error-fix-static-reference-static
相关文章推荐
- AndroidStudio下的点击事件不响应
- 分析Android-Universal-Image-Loader的缓存处理机制
- Android Studio 每次启动都默认打开项目--解决方法
- Android中关于图片压缩的小结
- AndroidStudio 开启lambda支持
- Android ListView 单条刷新方法实践及原理解析
- 图解Android Studio 2.0安装步骤
- android 实现类似知乎 上推隐藏头尾 下拉出现头尾的Demo
- android实现点击屏幕其他地方popupwindow消失
- Android焦点控制
- Android ListView的OnItemClickListener详解
- Android Studio 使用技巧汇总
- Android如何设置图标字体 IconFont(HTML图标)
- 安卓开发中的那点事
- Android中常见的内存溢出错误
- Android中AsyncTask的使用与源码分析
- android Studio快捷方式
- 基于Android的百度地图开发
- Android编程之ListView分页加载数据
- Error:(20, 45) 错误: <匿名com.example.administrator.androidthread.MainActivity$1>不是抽象的, 并且未覆盖Handler中的抽象