脚本宝典收集整理的这篇文章主要介绍了Android 获取 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), 上面方法可能会得到下面异常:
**java.lang.ClassCastException: andROId.support.v7.widget.TintContextWrapPEr cannot be cast to ...Activity**
问题原因:
原因其实很简单. 上 AppCompatTextView 的构造函数代码:
public class AppCompatTextView extends TextView implements TintableBackgroundView {
......
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.
网上有说 23.3.0 的 v7 之后的包中会有这个问题, 之前的不会. 我手头只有 25.3.0 的源码, 没有验证其他版本的代码是什么样子. 不过这不重要, 因为下面的解决方案可以兼容各个情况.
问题解决:
知道原因就简单了. 可以简单的用 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 的问题.
事实上, 谷歌 v7 包中的 android.support.v7.app.MediaRouteButton
就是这么干的.
参考:
以上是脚本宝典为你收集整理的Android 获取 Activity 时遇到 TintContextWrapper cannot be cast to 的问题全部内容,希望文章能够帮你解决Android 获取 Activity 时遇到 TintContextWrapper cannot be cast to 的问题所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。