android-fragments 不可转换的类型;无法将androidx.fragment.app.Fragment转换为com.google.ar.sceneform.ux.ArFragment

hlswsv35  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(212)

我正在开发一个AR测量应用程序,但是我在arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);上有一个错误。错误显示不可转换的类型;无法将“androidx.fragment.app.Fragment”强制转换为“com.google.ar.sceneform.ux. ArFragment '。对于这种情况,我该怎么办?
mainActivity.java
我已经导入了com. google. ar. sceneform. ux. ArFragment。

public class MainActivity extends AppCompatActivity {

    private ArFragment arFragment;
    @Override
    @SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
    }
}

activty_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
htzpubme

htzpubme1#

参考编号:Ar Fragment Blog
下面是一个AR片段在另一个片段中使用的例子。

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Nullable
import com.google.ar.core.*

class MyFragment : Fragment() {

    private lateinit var arFragment: MyARFragment
    private var arShowing: Boolean = false

    override fun onCreateView(
        inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_sample, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        arFragment = childFragmentManager.findFragmentById(R.id.ar_fragment) as MyARFragment
        arFragment.arSceneView.scene
            .addOnUpdateListener {
                onUpdateFrame()
            }
     }

    private fun onUpdateFrame() {
        val frame = arFragment.arSceneView.arFrame ?: return
        // If there is no frame, just return.
        val updatedAugmentedImages =
            frame.getUpdatedTrackables(
                AugmentedImage::class.java
            )
        for (augmentedImage in updatedAugmentedImages) {
            when (augmentedImage.trackingState) {
                TrackingState.TRACKING -> {
                    if(!arShowing){
                        val anchor =  arFragment.arSceneView.session!!.createAnchor(augmentedImage.centerPose)
                        val node =
                            MyViewNode(context)
                        node.setImage(augmentedImage, anchor)
                        arFragment.arSceneView.scene.addChild(node)
                        arShowing = true
                    }
                }
                TrackingState.STOPPED -> {
                    arShowing = false
                }
            }
        }
    }
}

相关问题