英文:
Gradle build error in Android Studio for Dependent Spinner
问题
以下是翻译好的部分:
package com.example.e_dukan
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.example.e_dukan.databinding.ActivityMainBinding
class Order : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_order)
// Binding Command for Dependent Spinner
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val arrayList_District = arrayListOf<String>("Hapur", "Meerut", "Noida")
val arrayAdapter_District = ArrayAdapter(
applicationContext,
R.layout.list_district,
arrayList_District
)
binding.spParent.adapter = arrayAdapter_District
// Spinner_district View Command
val spinner: Spinner = findViewById(R.id.spinner_district)
ArrayAdapter.createFromResource(
this,
R.array.spinner_district,
android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<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=".Order">
<!-- Your XML layout content goes here -->
</androidx.constraintlayout.widget.ConstraintLayout>
这些是主要的翻译部分。如果你需要更多帮助或解决 Gradle 构建问题,请随时提问。
英文:
I was writing a code for my mobile app.
My app is written in Kotlin instead of Java but if was say then I will Decompile in Java too.
My main motive currently is to add a dependent list view option in my app using Spinner option and databinding. So I created some spinners in my activity_order.xml.
This is my activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".Order">
<ImageView
android:id="@+id/imageView"
android:layout_width="138dp"
android:layout_height="96dp"
android:layout_marginEnd="280dp"
android:layout_marginRight="280dp"
android:layout_marginBottom="632dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/logo_cut" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="DISTRICT NAME :"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.059"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="LIQUOR TYPE :"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.054"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="SHOP NAME :"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.053"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="LIQUOR BRAND :"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.058"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUANTITY :"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5"
app:layout_constraintVertical_bias="0.193" />
<Spinner
android:id="@+id/spinner_district"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.192" />
<Spinner
android:id="@+id/spinner_type"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.623"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.309" />
<Spinner
android:id="@+id/spinner_shop"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.653"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.425" />
<Spinner
android:id="@+id/spinner_brand"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.545"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
<!--
<Spinner
android:id="@+id/spinner_quantity"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.698"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.66" />
-->
<Spinner
android:id="@+id/spinner_quantity"
android:layout_width="166dp"
android:layout_height="26dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.698"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.66" />
<Button
android:id="@+id/btn1"
android:layout_width="302dp"
android:layout_height="34dp"
android:layout_marginBottom="112dp"
android:background="@drawable/custom_btn_proceed"
android:text="PROCEED TO PAYMENT"
android:textColor="#FFFFFF"
android:textColorHint="#F44336"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:layout_constraintVertical_bias="0.315" />
</androidx.constraintlayout.widget.ConstraintLayout>
In this activity I wanted to first create a dependent list with spinner id spinner_district and all the other are dependent to the upper spinner.
So I first added the following code to my gradle file
viewBinding
{
enabled=true
}
and then started creating a Binding command for the list in my Kotlin file as follows
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val arrayList_District= arrayListOf<String>("Hapur","Meerut","Noida")
val arrayAdapter_District=ArrayAdapter(applicationContext,R.layout.list_district,arrayList_District)
binding.spParent.adapter=arrayAdapter_District
This is my full kotlin file for the activity_order.xml named as Order.kt
package com.example.e_dukan
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.example.e_dukan.databinding.ActivityMainBinding
class Order : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_order)
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Binding Command for Dependent Spinner/////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val arrayList_District= arrayListOf<String>("Hapur","Meerut","Noida")
val arrayAdapter_District=ArrayAdapter(applicationContext,R.layout.list_district,arrayList_District)
binding.spParent.adapter=arrayAdapter_District
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Spinner_district View Command///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
val spinner: Spinner = findViewById(R.id.spinner_district)
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter.createFromResource(
this,
R.array.spinner_district,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
spinner.adapter = adapter
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
/*
class Order : Activity(), AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
val spinner: Spinner = findViewById(R.id.spinner_district)
spinner.onItemSelectedListener = this
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
*/
But when ever i build the gradle it shows the following error
Executing tasks: [:app:assembleDebug] in project A:\E Dukan
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:dataBindingMergeDependencyArtifactsDebug UP-TO-DATE
> Task :app:dataBindingMergeGenClassesDebug UP-TO-DATE
> Task :app:generateDebugResValues UP-TO-DATE
> Task :app:generateDebugResources UP-TO-DATE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:mainApkListPersistenceDebug UP-TO-DATE
> Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
> Task :app:extractDeepLinksDebug UP-TO-DATE
> Task :app:processDebugManifest UP-TO-DATE
> Task :app:mergeDebugShaders UP-TO-DATE
> Task :app:compileDebugShaders UP-TO-DATE
> Task :app:generateDebugAssets UP-TO-DATE
> Task :app:mergeDebugAssets UP-TO-DATE
> Task :app:processDebugJavaRes NO-SOURCE
> Task :app:mergeDebugResources
> Task :app:dataBindingGenBaseClassesDebug
> Task :app:processDebugResources
> Task :app:compileDebugKotlin FAILED
e: A:\E Dukan\app\src\main\java\com\example\e_dukan\Order.kt: (11, 40): Unresolved reference: ActivityMainBinding
e: A:\E Dukan\app\src\main\java\com\example\e_dukan\Order.kt: (23, 23): Unresolved reference: ActivityMainBinding
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 9s
15 actionable tasks: 4 executed, 11 up-to-date
Please help me solve this gradle build problem.
答案1
得分: 0
生成的用于数据绑定的类将与布局名称相同。
> :未解析的引用:ActivityMainBinding
因此,您应该使用 ActivityOrderBinding 代替 ActivityMainBinding。
另一件事是将
> setContentView(R.layout.activity_order)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
替换为
val binding: ActivityOrderBinding =
DataBindingUtil.setContentView(this, R.layout.activity_order)
要获取视图,请使用
binding.root
要了解更多关于数据绑定的信息,请查看这个 代码实验教程。
英文:
The generated class to use in databinding will have the same name as layout name
> : Unresolved reference: ActivityMainBinding
So you should use ActivityOrderBinding instead of ActivityMainBinding
Another thing replace
> setContentView(R.layout.activity_order)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
with
val binding : ActivityOrderBinding =
DataBindingUtil.setContentView(this, R.layout.activity_order)
And to get the view use
binding.root
To learn more databinding take a look at this code lab tuto
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论