我已将我的AR片段从我的主要活动或它自己的片段中移出,以便可以将其交换进出。当我尝试从片段中获取AR片段时,它现在位于logcat中,告诉我该片段为null。当我从主要活动中运行该代码时,该代码有效。我想知道我是否必须在片段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>