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

AR位置评论项目调用ViewRenderable.build()触发崩溃求助

修复建议
  • 修正ViewRenderable.builder的setView参数错误
    原代码setView(x, )参数完全不符合方法要求:第一个参数必须传入Context实例,第二个参数是你自定义的评论项布局资源ID(比如存放在res/layout下的comment_item.xml,对应ID为R.layout.comment_item)。修正后示例:
    ViewRenderable.builder()
            // 第一个参数传入当前Activity的Context,第二个参数为自定义评论布局ID
            .setView(this, R.layout.comment_item)
            .build()
    
  • 修正thenAccept回调的传参类型错误
    原代码向placeComment方法传入了ArrayList<String>类型的comments,但placeComment第三个参数要求是String类型,应传入当前遍历到的单条评论文本x,修正后示例:
    .thenAccept(renderable -> placeComment(anchor, renderable, x));
    
  • 新增ViewRenderable构建过程的异常捕获逻辑
    AR Core的ViewRenderable构建过程容易出现资源加载、上下文失效等问题,不加异常处理会直接触发崩溃,需添加exceptionally分支处理异常:
    ViewRenderable.builder()
            .setView(this, R.layout.comment_item)
            .build()
            .thenAccept(renderable -> placeComment(anchor, renderable, x))
            .exceptionally(throwable -> {
                // 自行处理异常,比如打印日志、提示用户
                Log.e("ARComment", "构建ViewRenderable失败", throwable);
                return null;
            });
    
  • 确认布局资源匹配
    你调用findViewById(R.id.post)获取TextView,需要确保R.layout.comment_item布局中确实存在ID为R.id.post的TextView控件,否则会出现空指针崩溃。
  • 可选优化:预加载ViewRenderable模板
    如果需要频繁添加评论,不要每次都调用builder()重新构建,可以提前构建一个通用的ViewRenderable模板,后续添加评论时直接复制实例再修改文本,能大幅提升性能、减少构建失败概率。

内容的提问来源于stack exchange,提问作者Dalin Craig

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 10:27:02