【android】关于getBaseContext();this等-----项目中遇到的问题
来源:互联网 发布:cc网络验证模块源码 编辑:程序博客网 时间:2024/04/19 16:29
项目中要添加收藏功能,操作其实并不难:长按视频后弹出对话框,选择是否收藏,若选择是,则把视频的信息存入数据库中。在“我的收藏”页面中用listview载入数据库中的收藏的视频信息。
完成这个功能期间,写了如下代码
<span style="font-size:18px;">PpVodItem selectedlikes=(PpVodItem)view.getTag(); OperateDAO newdao =new OperateDAO(getBaseContext());newdao.insertVodIntoPlayLikes(selectedlikes);</span>之前在getBaseContext()的位置是用this代替,eclipse始终报错,使用getBaseContext()却能正常进行,困扰我的问题用getBaseContext()轻易解决了。其实获得context的方法至少有三种:
Context = this;
Context = getParent();
Context = getBaseContext();
Context = getParent();
三种方法的异同整理如下:
1.getApplicationContext ()方法在应用程序要被摧毁时,返回整个应用程序生命周期的应用上下文,生命周期是整个应用。
2.this应用上下文返回activity的当前上下文,属于activity。当它被摧毁时,activity也被摧毁。
3.getBaseContext()是 ContextWrapper中的方法,返回由构造函数指定或setBaseContext()设置的上下文。
4.getParent():若是子类,则返回子activity的上下文
throughout the life cycle.
getBasecontext() should not be used just use Context instead of it which is associated with the activity and could
possible be destroyed when the activity is destroyed.
getApplication() is available to Activity and Services only. Although in current Android Activity and Service
implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that
this will always be the case (for example, in a specific vendor implementation). So if you want the Application class you registered in the Manifest, you should never callgetApplicationContext() and cast it to your application, because it may not be the application instance (which you obviously experienced with the test framework).
getParent() returns object of the activity if the current view is a child..In other words returns the activity object hosting the child view when called within the child.
0 0
- 【android】关于getBaseContext();this等-----项目中遇到的问题
- Android 中 getApplicationContext()、this、getBaseContext()之间的区别
- android中getApplicationContext()、this、getApplication()、getBaseContext之间的区别
- Android 中几个Context的区别,getApplicationContext(),Activity.this,getBaseContext()
- Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext() 之间的区别
- this和super && getApplicationContext/getBaseContext/this等的用法
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- Android中Activity.this,getApplicationContext(),getBaseContext()和this详解
- 关于Android项目混淆遇到的问题
- getApplicationContext()、getBaseContext()、this的区别
- getApplicationContext()、getBaseContext()、this的区别
- 关于android微信QQ等平台分享功能实现中遇到的问题处理
- javascript---遇到关于this的相关问题(解决this)(持续更新中...)
- Android:getApplicationContext()、Activity.this、 getBaseContext()区别
- android getContext() , getApplicationContext() , getBaseContext() and “this”
- 分享一个非常不错的.net论坛,http://bbs.netluntan.com
- Wind咨讯南京高校算法竞赛试题:输入ID号,提取出生日期
- HDFS2.2.0集群的HA高可靠配置
- 各个版本的iphone QQ
- 信号处理函数sigaction()
- 【android】关于getBaseContext();this等-----项目中遇到的问题
- 读书笔记---触摸事件与手势识别
- 数据结构之顺序链表
- 哈哈哈哈哈
- 数据结构学习笔记-排序2
- linux下的stdin,stdout,stderr详解
- linux编程中显示文件名,函数名,行号
- win7-Wmvare 上安装 64 位linux
- JDBC操作