Jor*_*Gil 7 android classcastexception android-custom-view
当我创建一个自定义时View,在某些情况下我需要Context通过类的构造函数Activity强制转换类,为了使一些任务View直接在我的自定义View类中进行膨胀,我收到以下错误:
java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
Run Code Online (Sandbox Code Playgroud)
这是抛出此错误的行:
View headerView = ((Activity) context).getLayoutInflater().inflate(R.layout.fragment_history_list_header, null);
Run Code Online (Sandbox Code Playgroud)
看起来这个错误只发生在Eclipse试图膨胀要在XML编辑器中显示的视图时(不会在运行时发生).
有人知道如何解决它吗?
提前致谢.
将呼叫更改为以下内容.您获得类强制转换异常的原因是因为BridgeContext不是Activity类型.
View headerView = LayoutInflater.from(context).inflate(R.layout.fragment_history_list_header, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5733 次 |
| 最近记录: |