View Renderable build causes program to crash

Viewed 116

I am working on a project where people can leave comments in AR at specific locations. To test this I was trying to have it display some test messages.

public void getStarted(Anchor anchor){
    ArrayList<String> comments = new ArrayList<String>();
    comments.add("Have a nice day");
    for (String x : comments) {
        if (closeEnough()) {
            ViewRenderable.builder()
                    .setView(x, )
                    .build()
                    .thenAccept(renderable -> placeComment(anchor, renderable, comments));
        }
    }
}

I was able to test it and it crashes when you try to place the test view. It crashes when .build() is called. Do you have any suggestions for fixing this?

I'll add the function leading up to the one before.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (checkSystemSupport(this)) {
        Button button = (Button) findViewById(R.id.button);
        arCam = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.arCameraArea);
        arCam.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
            Anchor anchor = hitResult.createAnchor();
            getStarted((anchor));
        });
    }
}

And the one that does the placing and edit of the text.

private void placeComment(Anchor anchor, @NonNull ViewRenderable renderable, String comments) {
    TextView t = renderable.getView().findViewById(R.id.post);
    t.setText(comments);

    AnchorNode anchorNode = new AnchorNode(anchor);
    anchorNode.setParent(arCam.getArSceneView().getScene());
    TransformableNode model = new TransformableNode(arCam.getTransformationSystem());
    model.setParent(anchorNode);
    model.setRenderable(renderable);
    model.select();
}
1 Answers

I had the same issue, then found this: https://github.com/google-ar/sceneform-android-sdk/issues/574. If arfragment is not loaded by the moment renderable is built, then it's crashing. I've added postdelayed to check if it's really init error, and it worked, viewrenderable had been built without issues.

Related