gpt4 book ai didi

android - 无法解决 MainActivityBinding 以及绑定(bind)相关问题

转载 作者:行者123 更新时间:2023-11-30 00:36:47 24 4
gpt4 key购买 nike

我想做一个科学计算器。

我在这个网站上找到了一些有用的代码:http://www.androidauthority.com/build-a-calculator-app-721910/

然后我根据自己的目的修改了它。在此代码中,我发现使用了绑定(bind)方法,因此我按照本网站上的说明在我的 IDE 中进行了以下更改:https://developer.android.com/topic/libraries/data-binding/index.html

我对 build.gradle(module app) 进行了更改并且还对我的 MainActivity.java 代码进行了更改。

但是出现了以下错误:

Messages Gradle Build

MainActivity.java

package com.example.anant.scientificcalculator;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import static java.lang.Math.*;
import java.text.DecimalFormat;
//import com.example.anant.scientificcalculator.databinding.MainActivityBinding;
//import android.content.DialogInterface;
//import android.text.TextUtils;
//import android.view.View;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

private MainActivityBinding binding; //error in this line cannot resolve MainActivityBinding.

private static final char FACT = '!';
private static final char POWER = '^';
private static final String SQRT = "SQRT";

private static final String SIN = "SIN";
private static final String COS = "COS";
private static final String TAN = "TAN";
private static final String LOG = "LOG";

private static final char LEFTC = '(';
private static final char RIGHTC = ')';
private static final String EXP = "EXP";
private static final String PIE = "PIE";

private static final char ADD = '+';
private static final char SUBTRACT = '-';
private static final char MULTIPlY = '*';
private static final char DIVIDE = '/';

private char CURRENT_ACTION;
private String ADVANCE_ACTION;

private double valueOne = Double.NaN;
private double valueTwo;

private DecimalFormat decimalFormat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

decimalFormat = new DecimalFormat("#.##########");

MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);

//error in all the lines below saying cannot resolve button-----(where ---- denotes the specific terms of the button)

binding.buttonDot.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ".");
}
});

binding.buttonZero.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "0");
}
});

binding.buttonOne.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "1");
}
});

binding.buttonTwo.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "2");
}
});

binding.buttonThree.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "3");
}
});

binding.buttonFour.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "4");
}
});

binding.buttonFive.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "5");
}
});

binding.buttonSix.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "6");
}
});

binding.buttonSeven.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "7");
}
});

binding.buttonEight.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "8");
}
});

binding.buttonNine.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "9");
}
});

binding.buttonLeftC.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "(");
}
});

binding.buttonRightC.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ")");
}
});

binding.buttonExp.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = EXP;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "EXP");
binding.editText.setText(null);
}
});

binding.buttonPie.setOnCLickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = PIE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "PIE");
binding.editText.setText(null);
}
});

binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADD;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});

binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});

binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPlY;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});

binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVIDE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});

binding.buttonFact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = FACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "!");
binding.editText.setText(null);
}
});

binding.buttonPow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = POWER;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "^");
binding.editText.setText(null);
}
});

binding.buttonSqrt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SQRT;
binding.infoTextView.setText("SQRT(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});

binding.buttonSine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SIN;
binding.infoTextView.setText("SIN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});

binding.buttonCosine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = COS;
binding.infoTextView.setText("COS(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});

binding.buttonTangent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = TAN;
binding.infoTextView.setText("TAN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});

binding.buttonLog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = LOG;
binding.infoTextView.setText("LOG(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});

binding.buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});

binding.buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length()-1));
}
else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});

}

private void computeCalculation() {
if(!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);

if(CURRENT_ACTION == ADD)
valueOne = this.valueOne + valueTwo;
else if(CURRENT_ACTION == SUBTRACT)
valueOne = this.valueOne - valueTwo;
else if(CURRENT_ACTION == MULTIPlY)
valueOne = this.valueOne * valueTwo;
else if(CURRENT_ACTION == DIVIDE)
valueOne = this.valueOne / valueTwo;
else if(CURRENT_ACTION == FACT)
{
int i,valueThree = 1;
for(i=(int) valueOne;i>0;i--) //type conversion
{
valueThree *= i;
}
}
else if(CURRENT_ACTION == POWER)
valueOne = Math.pow(valueOne,valueTwo);
else if(ADVANCE_ACTION == SQRT)
valueOne = Math.sqrt(valueOne);
else if(ADVANCE_ACTION == SIN)
valueOne = Math.sin(valueOne);
else if(ADVANCE_ACTION == COS)
valueOne = Math.cos(valueOne);
else if(ADVANCE_ACTION == TAN)
valueOne = Math.tan(valueOne);
else if(ADVANCE_ACTION == LOG)
valueOne = Math.log(valueOne);
else if(ADVANCE_ACTION == PIE)
valueOne = Math.PI;
else if(ADVANCE_ACTION == EXP)
valueOne = Math.E;
}
else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
}
catch (Exception e){}
}
}

这是我的xml文件

ma​​in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anant.scientificcalculator.MainActivity">


<TextView
android:id="@+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:hint="0"
android:inputType="numberDecimal"
android:lines="2"
android:maxLines="2"
android:textAlignment="textEnd"
android:textColor="@android:color/black"
android:textSize="40sp" />

<Button
android:id="@+id/buttonFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="@string/buttonFact"
android:textSize="20sp" />

<Button
android:id="@+id/buttonPow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonFact"
android:text="@string/buttonPow"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSqrt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonPow"
android:text="@string/buttonSqrt"
android:textSize="20sp" />

<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/buttonSqrt"
android:layout_below="@id/editText"
android:text="@string/buttonClear"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFact"
android:text="@string/buttonSine"
android:textSize="20sp" />

<Button
android:id="@+id/buttonCosine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonPow"
android:layout_toRightOf="@id/buttonSine"
android:text="@string/buttonCosine"
android:textSize="20sp" />

<Button
android:id="@+id/buttonTangent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSqrt"
android:layout_toRightOf="@id/buttonCosine"
android:text="@string/buttonTangent"
android:textSize="20sp" />

<Button
android:id="@+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonClear"
android:layout_toRightOf="@id/buttonTangent"
android:text="@string/buttonLog"
android:textSize="20sp" />

<Button
android:id="@+id/buttonLeftC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSine"
android:text="@string/buttonLeftC"
android:textSize="20sp" />

<Button
android:id="@+id/buttonRightC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonCosine"
android:layout_toRightOf="@id/buttonLeftC"
android:text="@string/buttonRightC"
android:textSize="20sp" />

<Button
android:id="@+id/buttonExp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTangent"
android:layout_toRightOf="@id/buttonRightC"
android:text="@string/buttonExp"
android:textSize="20sp" />

<Button
android:id="@+id/buttonPie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLog"
android:layout_toRightOf="@id/buttonExp"
android:text="@string/buttonPie"
android:textSize="20sp" />


<Button
android:id="@+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLeftC"
android:text="@string/buttonSeven"
android:textSize="20sp" />

<Button
android:id="@+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonRightC"
android:layout_toRightOf="@id/buttonSeven"
android:text="@string/buttonEight"
android:textSize="20sp" />

<Button
android:id="@+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonExp"
android:layout_toRightOf="@id/buttonEight"
android:text="@string/buttonNine"
android:textSize="20sp" />

<Button
android:id="@+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSeven"
android:text="@string/buttonFour"
android:textSize="20sp" />

<Button
android:id="@+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonEight"
android:layout_toRightOf="@id/buttonFour"
android:text="@string/buttonFive"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonNine"
android:layout_toRightOf="@id/buttonFive"
android:text="@string/buttonSix"
android:textSize="20sp" />

<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFour"
android:text="@string/buttonOne"
android:textSize="20sp" />

<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFive"
android:layout_toRightOf="@id/buttonOne"
android:text="@string/buttonTwo"
android:textSize="20sp" />

<Button
android:id="@+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSix"
android:layout_toRightOf="@id/buttonTwo"
android:text="@string/buttonThree"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonOne"
android:text="@string/buttonDot"
android:textSize="20sp" />

<Button
android:id="@+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTwo"
android:layout_toRightOf="@id/buttonDot"
android:text="@string/buttonZero"
android:textSize="20sp" />

<Button
android:id="@+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/buttonNine"
android:layout_below="@id/buttonThree"
android:text="@string/buttonEqual"
android:textSize="20sp" />

<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonNine"
android:layout_toRightOf="@id/buttonNine"
android:text="@string/buttonDIvide"
android:textSize="20sp" />

<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonSix"
android:layout_toRightOf="@id/buttonSix"
android:text="@string/buttonMultiply"
android:textSize="20sp" />

<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonThree"
android:layout_toRightOf="@id/buttonThree"
android:text="@string/buttonSubtract"
android:textSize="20sp" />

<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonEqual"
android:layout_toRightOf="@id/buttonEqual"
android:text="@string/buttonAdd"
android:textSize="20sp" />

</RelativeLayout>

最佳答案

您需要取消注释:

//import com.example.anant.scientificcalculator.databinding.MainActivityBinding;

这是为您的数据绑定(bind)自动生成的类。你需要它。让它看起来像这样:

com.example.anant.scientificcalculator.databinding.MainActivityBinding;

其次,您的 XML 文件不正确,它应该将其 RelativeLayout 包裹在 layout 标记中,如下所示:

<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anant.scientificcalculator.MainActivity">
...
</layout>

在你这样做之后,进行清理和重建,我认为你应该可以开始了。

关于android - 无法解决 MainActivityBinding 以及绑定(bind)相关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303338/

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