Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

android studio - I have created a To do List app with a FAB and my code is returning three errors: Expecting ')', Expecting and Element and Unresolved Reference fab

I have created a To do List app with a Floating Actin Button and my code is returning three errors:

  • Expecting ')'
  • Expecting an Element
  • Unresolved Reference fab

The code was fine until I decided to add another button to the bottom of my activity_main.xml file and needed to add a relativelayout inside a coordinator layout to do it. The new button is to allow users to change the colour of the background. Once adding this code into the MainActivity.kt file the original findViewById code for the fab no longer works and gives the above errors.

MainActivity.kt file

abstract class MainActivity : AppCompatActivity() ,UpdateAndDelete {

    private lateinit var database: DatabaseReference
    var toDoList: MutableList<ToDoModel>? = null
    lateinit var adapter: ToDoAdapter
    private var listViewItem : ListView?=null
    internal abstract var screenView:View
    internal abstract var clickMe:Button
    internal abstract var color:Array<Int>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        intArrayOf(Color.MAGENTA, Color.YELLOW, Color.BLUE, Color.BLACK)
        screenView = findViewById(R.id.rView)
        clickMe = findViewById(R.id.colorButton) as Button
        clickMe.setOnClickListener(object:View.OnClickListener {
            override fun onClick(view: View) {
                val aryLength = color.size
                val random = Random
                val rNum = random.nextInt(aryLength)
                screenView.setBackgroundColor(color[rNum])
            }
        }
        val fab: View = findViewById(R.id.fab)
        listViewItem = findViewById<ListView>(R.id.item_listView)

        database = FirebaseDatabase.getInstance().reference

        fab.setOnClickListener { view ->
            val alertDialog = AlertDialog.Builder(this)
            val textEditText = EditText (this)
            alertDialog.setMessage("Add TODO Item")
            alertDialog.setTitle("Enter TO DO Item")
            alertDialog.setView(textEditText)
            alertDialog.setPositiveButton("Add") {dialog, i ->
                val todoItemData = ToDoModel.createList()
                todoItemData.itemDataText = textEditText.text.toString()
                todoItemData.done = false

                val newItemData=database.child("todo").push()
                todoItemData.UID = newItemData.key

                newItemData.setValue(todoItemData)

                dialog.dismiss()
                Toast.makeText(this, "item saved", Toast.LENGTH_LONG).show()
            }
            alertDialog.show()

        }

activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="#000000"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/item_listView"
        android:layout_width="match_parent"
        android:layout_height="651dp"
        android:background="@color/white"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:scrollbars="none" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_gravity="bottom|end"
        android:elevation="6dp"
        app:pressedTranslationZ="12dp"
        android:src="@drawable/ic_baseline_add_24" />

    <RelativeLayout
        android:id="@+id/rView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:gravity="bottom">

        <Button
            android:id="@+id/colorButton"
            android:layout_width="170dp"
            android:layout_height="58dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="22dp"
            android:layout_marginBottom="11dp"
            android:backgroundTint="@color/teal_200"
            android:text="Change Colour"
            android:textColor="@color/black" />

    </RelativeLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>
question from:https://stackoverflow.com/questions/65867883/i-have-created-a-to-do-list-app-with-a-fab-and-my-code-is-returning-three-errors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looking at this bit of your code:

clickMe.setOnClickListener(object:View.OnClickListener {
    override fun onClick(view: View) {
        val aryLength = color.size
        val random = Random
        val rNum = random.nextInt(aryLength)
        screenView.setBackgroundColor(color[rNum])
    }
}
val fab: View = findViewById(R.id.fab)

You are getting the first error because you are not properly closing the call to clickMe.setOnClickListener by providing a ) after the } on the second to last line. The compiler realizes this on the last line that defines fab, and so it is that line that the compiler is actually complaining about.

To fix this, add a ) to the second to last line, like this:

clickMe.setOnClickListener(object:View.OnClickListener {
    override fun onClick(view: View) {
        val aryLength = color.size
        val random = Random
        val rNum = random.nextInt(aryLength)
        screenView.setBackgroundColor(color[rNum])
    }
})
val fab: View = findViewById(R.id.fab)

Once you have an initial syntax error, it doesn't really matter what other errors the compiler is producing. Fix this first error and re-evaluate where you're at.

If the code you supply is supposed to be complete...that is, the entire contents of the file MainActivity.kt is provided, then you are missing a number of closing curlies (}) at the end of the code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...