Android studio 我创建了一个带有FAB的待办事项列表应用程序,我的代码返回三个错误:预期为';)';,预期和元素以及未解析的引用fab
我创建了一个带有浮动Actin按钮的待办事项列表应用程序,我的代码返回三个错误:Android studio 我创建了一个带有FAB的待办事项列表应用程序,我的代码返回三个错误:预期为';)';,预期和元素以及未解析的引用fab,android-studio,kotlin,Android Studio,Kotlin,我创建了一个带有浮动Actin按钮的待办事项列表应用程序,我的代码返回三个错误: 期待“)” 需要一个元素 未解析参考晶片 代码很好,直到我决定在activity_main.xml文件的底部添加另一个按钮,并且需要在协调器布局中添加relativelayout来执行此操作。新按钮允许用户更改背景颜色。一旦将此代码添加到MainActivity.kt文件中,fab的原始findViewById代码将不再工作,并给出上述错误 MainActivity.kt文件 abstract class Ma
- 期待“)”
- 需要一个元素
- 未解析参考晶片
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()
}
抽象类MainActivity:AppCompativeActivity(),UpdateAndDelete{
私有lateinit变量数据库:DatabaseReference
变量toDoList:可变列表?=null
lateinit var适配器:ToDoAdapter
私有变量listViewItem:ListView?=null
内部抽象变量屏幕视图:视图
内部抽象变量clickMe:按钮
内部抽象变量颜色:数组
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
凹版(颜色.洋红,颜色.黄色,颜色.蓝色,颜色.黑色)
screenView=findViewById(R.id.rView)
clickMe=findViewById(R.id.colorButton)作为按钮
clickMe.setOnClickListener(对象:View.OnClickListener{
覆盖趣味onClick(视图:视图){
val aryLength=color.size
val random=随机
val rNum=random.nextInt(aryLength)
screenView.setBackgroundColor(颜色[rNum])
}
}
val fab:View=findViewById(R.id.fab)
listViewItem=findViewById(R.id.item\u listView)
database=FirebaseDatabase.getInstance().reference
fab.setOnClickListener{view->
val alertDialog=alertDialog.Builder(此)
val text EditText=EditText(此)
alertDialog.setMessage(“添加待办事项”)
alertDialog.setTitle(“输入待办事项”)
alertDialog.setView(文本编辑文本)
alertDialog.setPositiveButton(“添加”){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.discover()的
Toast.makeText(此“项目已保存”,Toast.LENGTH\u LONG.show())
}
alertDialog.show()
}
activity_main.xml文件
<?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>
看看你的这段代码:
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)
出现第一个错误是因为您没有通过在}之后提供)
来正确关闭对clickMe.setOnClickListener的调用
在倒数第二行。编译器在定义fab
的最后一行实现了这一点,因此编译器实际上是在抱怨这一行
要解决此问题,请在倒数第二行添加一个)
,如下所示:
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)
一旦出现初始语法错误,编译器产生的其他错误就无关紧要了。请修复第一个错误并重新评估您所处的位置
如果您提供的代码应该是完整的……也就是说,提供了文件MainActivity.kt
的全部内容,那么您在代码末尾缺少了一些结束卷曲(}
)。谢谢Steve。这已经修复了代码,现在我没有错误,但应用程序不会打开。每次我运行应用程序时,它都会尝试打开,然后闪烁屏幕,然后再次关闭。我已经卸载并重新安装了仿真器,并从仿真器中删除了数据,但它一直崩溃。有什么想法吗?