Gradle构建错误:Android Studio中的依赖Spinner

huangapple go评论67阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.Order&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/imageView&quot;
android:layout_width=&quot;138dp&quot;
android:layout_height=&quot;96dp&quot;
android:layout_marginEnd=&quot;280dp&quot;
android:layout_marginRight=&quot;280dp&quot;
android:layout_marginBottom=&quot;632dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:srcCompat=&quot;@drawable/logo_cut&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;136dp&quot;
android:text=&quot;DISTRICT NAME :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.059&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView3&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;60dp&quot;
android:text=&quot;LIQUOR TYPE :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.054&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView2&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView4&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;56dp&quot;
android:text=&quot;SHOP NAME :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.053&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView3&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView5&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;60dp&quot;
android:text=&quot;LIQUOR BRAND :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.058&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView4&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView6&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;QUANTITY :&quot;
android:textColor=&quot;#000000&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.05&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/textView5&quot;
app:layout_constraintVertical_bias=&quot;0.193&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinner_district&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView2&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.192&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinner_type&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.623&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView3&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.309&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinner_shop&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.653&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView4&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.425&quot; /&gt;
&lt;Spinner
android:id=&quot;@+id/spinner_brand&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.545&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView5&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.541&quot; /&gt;
&lt;!--
&lt;Spinner
android:id=&quot;@+id/spinner_quantity&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.698&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView6&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.66&quot; /&gt;
--&gt;
&lt;Spinner
android:id=&quot;@+id/spinner_quantity&quot;
android:layout_width=&quot;166dp&quot;
android:layout_height=&quot;26dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginRight=&quot;16dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.698&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/textView6&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.66&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btn1&quot;
android:layout_width=&quot;302dp&quot;
android:layout_height=&quot;34dp&quot;
android:layout_marginBottom=&quot;112dp&quot;
android:background=&quot;@drawable/custom_btn_proceed&quot;
android:text=&quot;PROCEED TO PAYMENT&quot;
android:textColor=&quot;#FFFFFF&quot;
android:textColorHint=&quot;#F44336&quot;
android:textSize=&quot;24sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.498&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/imageView3&quot;
app:layout_constraintVertical_bias=&quot;0.315&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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&lt;String&gt;(&quot;Hapur&quot;,&quot;Meerut&quot;,&quot;Noida&quot;)
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&lt;String&gt;(&quot;Hapur&quot;,&quot;Meerut&quot;,&quot;Noida&quot;)
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 -&gt;
// 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&lt;*&gt;, 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&lt;*&gt;) {
// Another interface callback
}
}
*/

But when ever i build the gradle it shows the following error

Executing tasks: [:app:assembleDebug] in project A:\E Dukan
&gt; Task :app:preBuild UP-TO-DATE
&gt; Task :app:preDebugBuild UP-TO-DATE
&gt; Task :app:compileDebugRenderscript NO-SOURCE
&gt; Task :app:compileDebugAidl NO-SOURCE
&gt; Task :app:dataBindingMergeDependencyArtifactsDebug UP-TO-DATE
&gt; Task :app:dataBindingMergeGenClassesDebug UP-TO-DATE
&gt; Task :app:generateDebugResValues UP-TO-DATE
&gt; Task :app:generateDebugResources UP-TO-DATE
&gt; Task :app:generateDebugBuildConfig UP-TO-DATE
&gt; Task :app:mainApkListPersistenceDebug UP-TO-DATE
&gt; Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
&gt; Task :app:extractDeepLinksDebug UP-TO-DATE
&gt; Task :app:processDebugManifest UP-TO-DATE
&gt; Task :app:mergeDebugShaders UP-TO-DATE
&gt; Task :app:compileDebugShaders UP-TO-DATE
&gt; Task :app:generateDebugAssets UP-TO-DATE
&gt; Task :app:mergeDebugAssets UP-TO-DATE
&gt; Task :app:processDebugJavaRes NO-SOURCE
&gt; Task :app:mergeDebugResources
&gt; Task :app:dataBindingGenBaseClassesDebug
&gt; Task :app:processDebugResources
&gt; 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 &#39;:app:compileDebugKotlin&#39;.
&gt; 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

huangapple
  • 本文由 发表于 2020年5月5日 16:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61608801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定