android.widget.LinearLayout 无法通过视图绑定转换为 android.widget.GridLayout
android.widget.LinearLayout cannot be cast to android.widget.GridLayout with view binding
如果您发现问题所在,请提供帮助!
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
lateinit var buttonBinding: LayoutButtonBinding
lateinit var textBinding: LayoutTextBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
textBinding = LayoutTextBinding.bind(binding.root)
buttonBinding = LayoutButtonBinding.bind(binding.root)
buttonBinding.btnOne.setOnClickListener {
Toast.makeText(this, "Button Clear Clicked", Toast.LENGTH_SHORT).show()
}
}
}
activity_main.xml
<LinearLayout 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"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<include
layout="@layout/layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<include
layout="@layout/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>
layout_button.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnCount="4"
android:orientation="vertical"
android:rowCount="5">
<Button
android:id="@+id/btn_clear"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="0"
android:text="C" />
<Button
android:id="@+id/btn_percentage"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="0"
android:text="%" />
<Button
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="0"
android:text="B" />
<Button
android:id="@+id/btn_divide"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="0"
android:text="/" />
<Button
android:id="@+id/btn_seven"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="1"
android:text="7" />
<Button
android:id="@+id/btn_eight"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="1"
android:text="8" />
<Button
android:id="@+id/btn_nine"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="1"
android:text="9" />
<Button
android:id="@+id/btn_multply"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="1"
android:text="x" />
<Button
android:id="@+id/btn_six"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="2"
android:text="6" />
<Button
android:id="@+id/btn_five"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="2"
android:text="5" />
<Button
android:id="@+id/btn_four"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="2"
android:text="4" />
<Button
android:id="@+id/btn_minus"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="2"
android:text="-" />
<Button
android:id="@+id/btn_three"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="3"
android:text="3" />
<Button
android:id="@+id/btn_two"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="3"
android:text="2" />
<Button
android:id="@+id/btn_one"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="3"
android:text="1" />
<Button
android:id="@+id/btn_plus"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="3"
android:text="+" />
<Button
android:id="@+id/btn_equal"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="4"
android:text="=" />
<Button
android:id="@+id/btn_doubleZero"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="4"
android:text="00" />
<Button
android:id="@+id/btn_zero"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="4"
android:text="0" />
<Button
android:id="@+id/btn_dot"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="4"
android:text="." />
</GridLayout>
layout_text.xml
<?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/white"
android:orientation="vertical">
<TextView
android:id="@+id/textViewCalculations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1+2+2"
android:textAlignment="textEnd"
android:textSize="30sp" />
<TextView
android:id="@+id/textCalResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="40"
android:textAlignment="textEnd"
android:textSize="40sp" />
</LinearLayout>
遇到的问题:应用程序崩溃并出现异常:在 [=49= 中绑定 button_layout 时,无法将 android.widget.LinearLayout 转换为 android.widget.GridLayout ] 在第 21 行
buttonBinding = LayoutButtonBinding.bind(binding.root)
这不是您应该使用 LayoutButtonBinding
和 LayoutTexBinding
的方式。
我们需要为包含的部分分配一个 id,这样在 ActivityMainBinding 中我们将可以访问它们各自的绑定。
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.layoutButton.btnOne.setOnClickListener {
Toast.makeText(this, "Button Clear Clicked", Toast.LENGTH_SHORT).show()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/layout_text"
layout="@layout/layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<include
android:id="@+id/layout_button"
layout="@layout/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
如果您发现问题所在,请提供帮助!
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
lateinit var buttonBinding: LayoutButtonBinding
lateinit var textBinding: LayoutTextBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
textBinding = LayoutTextBinding.bind(binding.root)
buttonBinding = LayoutButtonBinding.bind(binding.root)
buttonBinding.btnOne.setOnClickListener {
Toast.makeText(this, "Button Clear Clicked", Toast.LENGTH_SHORT).show()
}
}
}
activity_main.xml
<LinearLayout 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"
android:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity">
<include
layout="@layout/layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<include
layout="@layout/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>
layout_button.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnCount="4"
android:orientation="vertical"
android:rowCount="5">
<Button
android:id="@+id/btn_clear"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="0"
android:text="C" />
<Button
android:id="@+id/btn_percentage"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="0"
android:text="%" />
<Button
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="0"
android:text="B" />
<Button
android:id="@+id/btn_divide"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="0"
android:text="/" />
<Button
android:id="@+id/btn_seven"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="1"
android:text="7" />
<Button
android:id="@+id/btn_eight"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="1"
android:text="8" />
<Button
android:id="@+id/btn_nine"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="1"
android:text="9" />
<Button
android:id="@+id/btn_multply"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="1"
android:text="x" />
<Button
android:id="@+id/btn_six"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="2"
android:text="6" />
<Button
android:id="@+id/btn_five"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="2"
android:text="5" />
<Button
android:id="@+id/btn_four"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="2"
android:text="4" />
<Button
android:id="@+id/btn_minus"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="2"
android:text="-" />
<Button
android:id="@+id/btn_three"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="3"
android:text="3" />
<Button
android:id="@+id/btn_two"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="3"
android:text="2" />
<Button
android:id="@+id/btn_one"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="3"
android:text="1" />
<Button
android:id="@+id/btn_plus"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="3"
android:text="+" />
<Button
android:id="@+id/btn_equal"
style="@style/CalButtonArithmatic"
android:layout_column="3"
android:layout_row="4"
android:text="=" />
<Button
android:id="@+id/btn_doubleZero"
style="@style/CalButtonFunctional"
android:layout_column="0"
android:layout_row="4"
android:text="00" />
<Button
android:id="@+id/btn_zero"
style="@style/CalButtonFunctional"
android:layout_column="1"
android:layout_row="4"
android:text="0" />
<Button
android:id="@+id/btn_dot"
style="@style/CalButtonFunctional"
android:layout_column="2"
android:layout_row="4"
android:text="." />
</GridLayout>
layout_text.xml
<?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/white"
android:orientation="vertical">
<TextView
android:id="@+id/textViewCalculations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1+2+2"
android:textAlignment="textEnd"
android:textSize="30sp" />
<TextView
android:id="@+id/textCalResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="40"
android:textAlignment="textEnd"
android:textSize="40sp" />
</LinearLayout>
遇到的问题:应用程序崩溃并出现异常:在 [=49= 中绑定 button_layout 时,无法将 android.widget.LinearLayout 转换为 android.widget.GridLayout ] 在第 21 行
buttonBinding = LayoutButtonBinding.bind(binding.root)
这不是您应该使用 LayoutButtonBinding
和 LayoutTexBinding
的方式。
我们需要为包含的部分分配一个 id,这样在 ActivityMainBinding 中我们将可以访问它们各自的绑定。
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.layoutButton.btnOne.setOnClickListener {
Toast.makeText(this, "Button Clear Clicked", Toast.LENGTH_SHORT).show()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="@+id/layout_text"
layout="@layout/layout_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<include
android:id="@+id/layout_button"
layout="@layout/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>