gpt4 book ai didi

java - 如何修复 "Error: ' ;' expected"或 "Error: ' )' expected"以及 boolean 值中的错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:01 27 4
gpt4 key购买 nike

我正在尝试为 ExpenseName 和 ExpenseCost Edittext 创建验证,以便如果没有用户输入,它将提醒用户输入一些数据,并且 boolean 值中的“文本”也有错误,因为它是红色的。我从另一个 .java 复制了代码,所以我很确定它工作得很好

package com.example.back4app.userregistrationexample.Classes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.back4app.userregistrationexample.R;

public class ExpenseActivity extends AppCompatActivity {

private EditText ExpenseName;
private EditText ExpenseCost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense);

ExpenseName = (EditText) findViewById(R.id.txt_expensename);
ExpenseCost = (EditText) findViewById(R.id.txt_expensecost);

final Button btn_addexpense = findViewById(R.id.btn_addexpense);
btn_addexpense.setOnClickListener(new View.OnClickListener()); {
@Override
public void onClick(View v) {
//Validating the log in data
boolean validationError = false;

StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(ExpenseCost)) {
validationError = true;
validationErrorMessage.append("the cost of the expense");
}
if (isEmpty(ExpenseName)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("the name of the expense");
}
}
}

private boolean isEmpty(EditText text) {
if (text.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
}
}

我尝试将“(”和“)”放在“文本”之外,但它仍然是红色,我还尝试将“isEmpty”更改为任何其他名称,因为它可能正在另一个.java上读取,但仍然发生同样的事情。另外,我尝试重建项目和清理项目,但没有任何效果,我还尝试使缓存无效/重新启动,因为我读到有人说 Android 出现这样的错误并不罕见。我的代码有问题吗?

编辑:

好的。所以我关闭了 OnClickListener 但现在更多“错误:';'”出现“expected”并且“OnClickListener”是抽象的;无法实例化”

最佳答案

您没有在发布的代码中关闭 onCreatesetOnClickListener

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense);

ExpenseName = (EditText) findViewById(R.id.txt_expensename);
ExpenseCost = (EditText) findViewById(R.id.txt_expensecost);

final Button btn_addexpense = findViewById(R.id.btn_addexpense);
btn_addexpense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Validating the log in data
boolean validationError = false;

StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(ExpenseCost)) {
validationError = true;
validationErrorMessage.append("the cost of the expense");
}
if (isEmpty(ExpenseName)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("the name of the expense");
}
}
}); // here
}

private boolean isEmpty(EditText text) {
if (text.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}

关于java - 如何修复 "Error: ' ;' expected"或 "Error: ' )' expected"以及 boolean 值中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54614112/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com