Android 从相机按钮启动 Sceneform Activity

Android Launch Sceneform Activity from Camera Button

我正在使用 Sceneform 创建一个应用程序,当我按下相机按钮时,我希望用户被导航到我包含在另一个 activity 中的 ArFragment。 PoiPaintingARCamera Activity 如果在新的 activity 中单独实现,则可以完美运行。 Sample Photo of the Activity

这是我在 PoiPainting 中使用的代码 Activity

btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(PoiPainting.this, PoiPaintingArCamera.class);
            startActivity(intent);
        }

PoiPaintingAR 代码Activity

public class PoiPaintingArCamera extends AppCompatActivity {

private ExternalTexture texture;
private MediaPlayer mediaPlayer;
private ArFragment arFragment;
private Scene scene;
private ModelRenderable renderable;
private boolean isImageDetected = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poi_painting_ar_camera);

    texture = new ExternalTexture();

    mediaPlayer = MediaPlayer.create(this, R.raw.video);
    mediaPlayer.setSurface(texture.getSurface());
    mediaPlayer.setLooping(true);

    ModelRenderable
            .builder()
            .setSource(this, Uri.parse("video_screen.sfb"))
            .build()
            .thenAccept(modelRenderable -> {
                modelRenderable.getMaterial().setExternalTexture("videoTexture",
                        texture);
                modelRenderable.getMaterial().setFloat4("keyColor",
                        new Color(0.01843f, 1f, 0.098f));

                renderable = modelRenderable;
            });

    arFragment = (com.stratos.syrostownhall.CustomArFragment)
            getSupportFragmentManager().findFragmentById(R.id.arFragment);

    if (arFragment != null) {
        scene = arFragment.getArSceneView().getScene();
    }

    scene.addOnUpdateListener(this::onUpdate);

}

private void onUpdate(FrameTime frameTime) {

    if (isImageDetected)
        return;

    Frame frame = arFragment.getArSceneView().getArFrame();

    Collection<AugmentedImage> augmentedImages =
            frame != null ? frame.getUpdatedTrackables(AugmentedImage.class) : null;


    for (AugmentedImage painting : Objects.requireNonNull(augmentedImages)) {
        if (painting.getTrackingState() == TrackingState.TRACKING) {

            if (painting.getName().equals("painting")) {

                isImageDetected = true;

                playVideo(painting.createAnchor(painting.getCenterPose()), painting.getExtentX(),
                        painting.getExtentZ());

                break;
            }

        }
    }

}

private void playVideo(Anchor anchor, float extentX, float extentZ) {

    mediaPlayer.start();

    AnchorNode anchorNode = new AnchorNode(anchor);

    texture.getSurfaceTexture().setOnFrameAvailableListener(surfaceTexture -> {
        anchorNode.setRenderable(renderable);
        texture.getSurfaceTexture().setOnFrameAvailableListener(null);
    });

    anchorNode.setWorldScale(new Vector3(extentX, 1f, extentZ));

    scene.addChild(anchorNode);

}

当我按下相机按钮时,应用程序崩溃并显示以下消息。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.stratos.syrostownhall, PID: 30431
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stratos.syrostownhall/com.stratos.syrostownhall.PoiPaintingArCamera}: java.lang.ClassCastException: com.google.ar.sceneform.ux.ArFragment cannot be cast to com.stratos.syrostownhall.CustomArFragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7397)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
 Caused by: java.lang.ClassCastException: com.google.ar.sceneform.ux.ArFragment cannot be cast to com.stratos.syrostownhall.CustomArFragment
    at com.stratos.syrostownhall.PoiPaintingArCamera.onCreate(PoiPaintingArCamera.java:59)

我有什么方法可以让它工作吗?

问题出在sceneform CustomArFragmentclass的anroid:name上。将名称更改为 android 目录解决了问题。