Java “如何修复”;错误:'';“预期”;或;错误:';)';“预期”;和布尔值中的错误

Java “如何修复”;错误:'';“预期”;或;错误:';)';“预期”;和布尔值中的错误,java,android-studio,Java,Android Studio,我正在尝试为ExpenseName和ExpenseCost Edittext创建验证,这样,如果没有用户输入,它将提醒用户输入一些数据,布尔值中的“文本”也有错误,因为它是红色的。我从另一个.java复制了代码,所以我很确定它工作得很好 package com.example.back4app.userregistrationexample.Classes; import android.support.v7.app.AppCompatActivity; import android.os.B

我正在尝试为ExpenseName和ExpenseCost Edittext创建验证,这样,如果没有用户输入,它将提醒用户输入一些数据,布尔值中的“文本”也有错误,因为它是红色的。我从另一个.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,但是现在出现了更多的“error:”;“expected”,并且“OnClickListener”是抽象的;无法实例化“

我很确定这是因为您的
.setOnClickListener
没有关闭

  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");
            }
        }
    });    //add a parentheses and semicolon to close the onClickListener

我很确定这是因为您的
.setOnClickListener
未关闭

  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");
            }
        }
    });    //add a parentheses and semicolon to close the onClickListener

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

@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;
    }
}

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

@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;
    }
}