如何修复 onClickListener

How to fix onClickListener

我有此代码 MainActivity 和 activity_main。

问题出在 android 监视器上说我尝试在空对象引用上调用虚方法'void。我尝试了一切,但我无法修复它。希望有人能帮助我!

1.main_activity

https://pastebin.com/2D8sjzAp

  1. 主要活动

https://pastebin.com/dmQZwu3R

private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';

private char CURRENT_ACTION;

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

private DecimalFormat decimalFormat;

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

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

    Button buttonDot = (Button) findViewById(R.id.buttonDot);
    Button buttonZero = (Button) findViewById(R.id.buttonZero);
    Button buttonOne = (Button) findViewById(R.id.buttonOne);
    Button buttonTwo = (Button) findViewById(R.id.buttonTwo);
    Button buttonThree = (Button) findViewById(R.id.buttonThree);
    Button buttonFour = (Button) findViewById(R.id.buttonFour);
    Button buttonFive = (Button) findViewById(R.id.buttonFive);
    Button buttonSix = (Button) findViewById(R.id.buttonSix);
    Button buttonSeven = (Button) findViewById(R.id.buttonSeven);
    Button buttonEight = (Button) findViewById(R.id.buttonEight);
    Button buttonNine = (Button) findViewById(R.id.buttonNine);
    Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
    Button buttonSubtract = (Button) findViewById(R.id.buttonSubtract);
    Button buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
    Button buttonDivide = (Button) findViewById(R.id.buttonDivide);
    Button buttonClear = (Button) findViewById(R.id.buttonClear);
    Button buttonEqual = (Button) findViewById(R.id.buttonEqual);

    final EditText editText = (EditText) findViewById(R.id.editText);
    final TextView infoTextView = (TextView) findViewById(R.id.infoTextView);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        if(CURRENT_ACTION == ADDITION)
            valueOne = this.valueOne + valueTwo;
        else if(CURRENT_ACTION == SUBTRACTION)
            valueOne = this.valueOne - valueTwo;
        else if(CURRENT_ACTION == MULTIPLICATION)
            valueOne = this.valueOne * valueTwo;
        else if(CURRENT_ACTION == DIVISION)
            valueOne = this.valueOne / valueTwo;
    }
    else {
        try {
            EditText editText = (EditText) findViewById(R.id.editText);//potential
            valueOne = Double.parseDouble(editText.getText().toString());
        }
        catch (Exception e){}
    }


}
private void showIn2(){
    EditText editText = (EditText) findViewById(R.id.editText);
    valueTwo = Double.parseDouble(editText.getText().toString());

    editText.setText(null);



    editText.setText(Integer.toString((int) valueTwo,2));



}

}

绑定视图前设置布局视图;

Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_XML_file);//This is the line you are missing

OnCreate()

中添加这一行
 setContentView(R.layout.activity_main);

在 Android 环境中,activity 有两个基本构建块:

  • XML 布局(面向用户的屏幕)
  • 对应后端代码(写在JavaKotlin

创建 activity 时,您要做的第一件主要事情是 link 您的 XML 文件到相应的 Java 或 Kotlin 代码( Android 工作室会在创建 activity)

时为您执行此操作

因此,您应该在 link XML 文件的 onCreate() 方法中包含以下代码行:

setContentView(R.layout.activity_main);

您的 onCreate() 方法应如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // your code goes here
}