首页 > 解决方案 > 当我尝试从另一个片段膨胀 AR 片段时,它正在返回 null

问题描述

我已将我的 AR 片段从我的主要活动或它自己的片段中移出,以便可以换入和换出。当我尝试从片段中获取 AR 片段时,它现在在 logcat 中告诉我片段为空。当我从我的主要活动中运行它时,此代码有效。我想知道我是否必须在片段 onViewCreated 之前初始化“ar_sceneform_fragment_view”,但我不确定如何。

这是片段类

class ArFragment : Fragment() {
    //lateinit for Augmented Reality Fragment
    private lateinit var arFragment: ArFragment
    //lateinit for the model uri
    private lateinit var selectedObject: Uri

    private var dialogStep: Int = 0

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ar, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Init Ar Fragment
        arFragment = activity?.supportFragmentManager?.findFragmentById(ar_sceneform_fragment_view.id) as ArFragment

        //Default model
        setModelPath("lion.sfb")

        //Tap listener for the ArFragment
        arFragment.setOnTapArPlaneListener { hitResult, plane, _ ->
            Log.d(TAG, "arFragment: onTapPlaneListener: Called")
            //If the surface is not Horizontal and upward facing
            if(plane.type == Plane.Type.HORIZONTAL_DOWNWARD_FACING) {
                Log.d(TAG, "arFragment: onTapPlanListener bottom surface found ")
                //return for the callback
                return@setOnTapArPlaneListener
            }

            //create a new anchor
            Log.d(TAG, "arFragment: onTapPlanListener: creating new anchor")
            val anchor = hitResult.createAnchor()
            placeObject(arFragment,anchor,selectedObject)
            if ( selectedObject == Uri.parse("gibbet_arcore.sfb"))
            {
                dialogStep = 0
                ar_adventure_text_layout.visibility = View.VISIBLE
                ar_adventure_button.setText(R.string.lbl_next)
                dealWithDialog()
            }
        }
    }
}

这是片段布局

<androidx.constraintlayout.widget.ConstraintLayout
        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=".ArFragment">

    <fragment
            android:id="@+id/ar_sceneform_fragment_view"
            android:name="com.google.ar.sceneform.ux.ArFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/ar_adventure_text_layout"
            android:layout_marginTop="0dp"/>

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/ar_adventure_text_layout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/ar_gallery_layout"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginBottom="4dp"
            app:layout_constraintHorizontal_bias="1.0"
            android:visibility="gone">

        <TextView
                android:id="@+id/ar_adventure_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                tools:visibility="visible"
                tools:text="Adventure text"
                app:layout_constraintTop_toTopOf="@+id/ar_adventure_text_layout"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>
        <Button
                android:id="@+id/ar_adventure_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ar_adventure_text"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <LinearLayout
            android:id="@+id/ar_gallery_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="0dp">

        <ImageView
                android:id="@+id/lion"
                android:layout_height="60dp"
                android:layout_width="60dp"
                android:src="@drawable/lion"
                android:contentDescription="@string/lbl_lion"
        />

        <ImageView
                android:id="@+id/dion"
                android:layout_height="60dp"
                android:layout_width="60dp"
                android:src="@drawable/dion"
                android:contentDescription="@string/lbl_dino"
        />
        <ImageView
                android:id="@+id/gibbit"
                android:layout_height="60dp"
                android:layout_width="60dp"
                android:src="@drawable/cage"
                android:contentDescription="@string/lbl_gibbit"
        />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-fragmentskotlinarcore

解决方案


推荐阅读