我想製作一個科學計算器。無法解析MainActivityBinding並綁定相關問題
我發現本網站上的一些有用的代碼:http://www.androidauthority.com/build-a-calculator-app-721910/
然後我修改爲我的目的。 在此代碼我發現採用裝訂方法,所以我提出在我的IDE以下變化按照本網站上的操作的指令:https://developer.android.com/topic/libraries/data-binding/index.html
我提出在的build.gradle(模塊的應用程序)的變化 和所做的更改我MainActivity.java代碼也是。
但出現以下錯誤:
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文件
main_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>
他們使用一些輔助類,即DataBinding和MainActivityBinding。所以你必須將這個類放入你的包中。 –