android.support.v7.widget.TintContextWrapper cannot be cast to ***
来源:互联网 发布:macbook下不了软件 编辑:程序博客网 时间:2024/06/06 18:48
- Android 从 View 中获取 Activity 时遇到 TintContextWrapper cannot be cast to 的问题
如果一个 View 绘制于某个 Activity 的 ContentView 上, 那它的 Context 一定是和这个 Activity 相关联的. 因此我们想在 View 中直接用 Activity 方法时 (最常用的应该就是 Activity.startActivity() 方法了), 不必再向 View 中传递 Activity 对象.
一般在 View 中获取这个 Activity 对象都是简单的用下面代码就可以了:
Activity activity = (Activity) getContext();
- 但在View继承自AppCompat系的View时(比如AppCompatTextView, AppCompatImageView), 上面方法可能会得到下面异常:
原因:
了解一下 AppCompatTextView 的构造函数代码:
public class AppCompatTextView extends TextView implements TintableBackgroundView { private AppCompatBackgroundHelper mBackgroundTintHelper; private AppCompatTextHelper mTextHelper; public AppCompatTextView(Context context) { this(context, null); } public AppCompatTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { //注意这一行 super(TintContextWrapper.wrap(context), attrs, defStyleAttr); mBackgroundTintHelper = new AppCompatBackgroundHelper(this); mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); mTextHelper = AppCompatTextHelper.create(this); mTextHelper.loadFromAttributes(attrs, defStyleAttr); mTextHelper.applyCompoundDrawablesTints();} ......}
在构造函数中, context 被用 TintContextWrapper 包了一层.。 所以这时 Activity 其实被保存在了 TintContextWrapper 中的 BaseContext 中了。即 TintContextWrapper 不能直接强制转换为 Activity。
- 问题解决方案
可以简单的用 TintContextWrapper.getBaseContext() 得到这个 Activity。
但其实一层层的从 ContextWrapper 中把 Activity 剥出来更保险:
/** * try get host activity from view. * views hosted on floating window like dialog and toast will sure return null. * @return host activity; or null if not available */ public static Activity getActivityFromView(View view) { Context context = view.getContext(); while (context instanceof ContextWrapper) { if (context instanceof Activity) { return (Activity) context; } context = ((ContextWrapper) context).getBaseContext(); } return null; }
此方法可以解决从 View 中获取 Activity 的兼容问题。
参考资料:https://stackoverflow.com/questions/37196284/android-support-library-23-4-0-android-support-v7-widget-tintcontextwrapper-can
阅读全文
0 0
- android.support.v7.widget.TintContextWrapper cannot be cast to ***
- android.support.v7.widget.AppCompatTextView cannot be cast to android.app.Activity
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- android.widget.HeaderViewListAdapter cannot be cast to ***
- Android 从 View 中获取 Activity 时遇到 TintContextWrapper cannot be cast to 的问题
- can not cast to android.support.v7.widget.ShareActionProvider
- android.widget.Button cannot be cast to android.widget.Gallery
- widget.Button cannot be cast to android.widget.ListView
- [android] cannot be cast to android.widget.HeaderViewListAdapter
- LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
- .ClassCastException: .LinearLayout cannot be cast to android.widget.TextView
- Error: cannot be cast to android.widget.HeaderViewListAdapter
- mybatis逆向工程
- 详解$_SERVER 函数中QUERY_STRING、REQUEST_URI、SCRIPT_NAME和'PHP_SELF
- 拦截导弹问题(贪心算法)
- 当你还不能写出自己满意的程序时,你就不要去睡觉。
- 学生信息管理系统之函数的作用
- android.support.v7.widget.TintContextWrapper cannot be cast to ***
- Spring各个jar包作用
- css3 box-sizing属性
- 1006. 换个格式输出整数 (15)
- Java中getResourceAsStream的用法
- vue2.0学习之使用常量替代 Mutation 事件类型释义
- ListView中item中的checkBox
- h3c 实验设备型号整理 笔记
- 大数C++板子