我有一些布局与不同的结构,我使用作为场景布局,我有一个父布局,我想加载他们不同的过渡。
父布局看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/scene_1"/>
</FrameLayout>
</FrameLayout>
场景1是这样的
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wall">
<ImageView
android:id="@+id/image_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/background"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/image_foreground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/foreground"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
</FrameLayout>
然后当一些回调来自服务器,我想改变场景为scene_2,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/wall2">
<ProgressBar
android:id="@+id/pb_wall2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/wall2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/white"
android:text="Some text"/>
<Button
android:id="@+id/btn_do_smth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:text="Do Smth"/>
<Button
android:id="@+id/btn_go_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:text="Go back"/>
</LinearLayout>
我希望能够设置进度条文本视图和按钮的可见性属性,以及设置监听器按钮等。为了这个目的,我在我的活动文件中这样做:
...
private fun initWall2View() {
wall2View = findViewById(R.id.wall2)
pbWall2 = findViewById(R.id.pb_wall2)
wall2Text = findViewById(R.id.wall2_text)
btnDoSmth = findViewById(R.id.btn_do_smth)
btnGoBack = findViewById(R.id.btn_go_back)
val initWall2ViewScene = Scene(rootScene, wall2View as ViewGroup)
TransitionManager.go(initWall2ViewScene, AutoTransition())
myViewModel.wall2State.observe(this) { wall2State ->
wall2State?.let {
when (it) {
MyViewModel.Wall2State.Loading -> {
wall2View.visibility = View.VISIBLE
pbWall2.visibility = View.VISIBLE
wall2Text.visibility = View.GONE
btnDoSmth.visibility = View.GONE
btnGoBack.visibility = View.GONE
}
MyViewModel.Wall2State.Show -> {
wall2View.visibility = View.VISIBLE
pbWall2.visibility = View.GONE
wall2Text.visibility = View.VISIBLE
btnDoSmth.visibility = View.VISIBLE
btnGoBack.visibility = View.VISIBLE
}
MyViewModel.Wall2State.Hide -> {
wall2View.visibility = View.GONE
}
}
}
}
btnDoSmth.setOnClickListener {
...
}
btnGoBack.setOnClickListener {
myViewModel.onGoBackPressed()
}
}
...
程序构建并开始,但是因为一个异常而终止:java。lang。NullPointerException: findViewById(R。id。wall2)不能为空
我猜这是因为没有字面上的元素与这样的id在布局xml文件后编译时间,但我如何正确地做这一点?我不想把所有的场景都放在一个xml文件中,因为我有很多场景,而且它们在不同的情况下有更多的逻辑。我希望有个解决办法。