You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用textview.getPaint()报错Binary XML第21行Error inflating class fragment

排查方向

  • 检查代码执行时机:Fragment的控件查找逻辑必须放在onCreateView方法中,且需要先完成根布局inflate操作再调用findViewById,如果在onCreate等更早的生命周期执行控件操作,会触发填充失败。
  • 检查控件ID一致性:确认布局XML中对应TextView的android:id属性值为@+id/textview,拼写、大小写完全匹配,否则findViewById会返回null,后续调用控件方法触发空指针导致填充错误。
  • 检查版本兼容:EmbossMaskFilter最低支持API 14,若项目minSdkVersion低于14,低版本设备运行会直接崩溃。
  • 检查XML第21行Fragment声明:如果是静态注册的Fragment,必须配置完整的类路径android:name="你的包名.对应Fragment类名",类名或路径错误会直接触发该报错。

正确代码示例(写在Fragment的onCreateView方法中)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // 先加载Fragment根布局,替换成你自己的布局文件名
    View root = inflater.inflate(R.layout.fragment_your_layout_name, container, false);
    // 根布局加载完成后再查找控件
    TextView textview= (TextView) root.findViewById(R.id.textview);
    // 增加空判避免空指针崩溃
    if (textview != null) {
        EmbossMaskFilter embossmaskfilter = new EmbossMaskFilter(new float[]{1,5,1},
                0.5f,
                10,
                7.5f);
        textview.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        textview.getPaint().setMaskFilter(embossmaskfilter);
    }
    return root;
}

内容的提问来源于stack exchange,提问作者Paul J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 12:54:05