尝试从 class 到 Main Activity onclick 方法获取计算结果以设置文本字段时,onClick 方法执行错误

onClick method execution error while trying to get calculation result from a class to Main Activity onclick method to set a textfield

我试图创建一个 class 来进行特定计算。变量应取自 Main Activity,结果应取自 class,文本字段应通过 Main Activity 中的单击方法设置。但是,从错误消息中我了解到应用程序崩溃是因为内部调用导致无法执行单击方法。我已经尝试对我的代码进行一些更改,但我仍然无法克服这个错误。

主要Activity代码是:

 public class MainActivity extends AppCompatActivity {

//Viewportların tanımları
EditText pressure, corrosionAllowance, result, allowableStress, materialDiameter, materialThickness, jointFactor;
//Objecte aktarılacak variable'lar
String designPressureS, corrosionAllowanceS, allowableStressS, materialDiameterS, materialThicknessS, jointFactorS;
Shell shell;

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

    //Viewprotlara atıyorum
    pressure = findViewById(R.id.pressure);
    designPressureS = String.valueOf(pressure);

    corrosionAllowance = findViewById(R.id.corrosionAllowance);
    corrosionAllowanceS = String.valueOf(corrosionAllowance);

    allowableStress = findViewById(R.id.allowableStress);
    allowableStressS = String.valueOf(allowableStress);

    materialDiameter = findViewById(R.id.materialDiameter);
    materialDiameterS = String.valueOf(materialDiameter);

    materialThickness = findViewById(R.id.materialThickness);
    materialThicknessS = String.valueOf(materialThickness);

    jointFactor = findViewById(R.id.jointFactor);
    jointFactorS = String.valueOf(jointFactor);

    result = findViewById(R.id.result);

    shell = new Shell(allowableStressS,materialDiameterS,materialThicknessS,jointFactorS,designPressureS,corrosionAllowanceS);


}

public void calculation(View view){

    String resultS = String.format("%.2f",shell.thickness());
    result.setText(resultS);

}

} 这是 class:

public class Shell {

String materialSMYS, outerDiameter, materialThickness, jointFactor, pressure, corrosionAllowance;
double designPressureD, corrosionAllowanceD, allowableStressD, materialDiameterD, materialThicknessD, jointFactorD, result;

public Shell(String materialSMYS, String outerDiameter, String materialThickness, String jointFactor, String pressure, String corrosionAllowance){

this.materialSMYS = materialSMYS;
this.outerDiameter = outerDiameter;
this.materialThickness = materialThickness;
this.jointFactor = jointFactor;
this.pressure = pressure;
this.corrosionAllowance = corrosionAllowance;

}

public double thickness(){

designPressureD = Double.parseDouble(pressure);
corrosionAllowanceD = Double.parseDouble(corrosionAllowance);
allowableStressD = Double.parseDouble(materialSMYS);
materialDiameterD = Double.parseDouble(outerDiameter);
materialThicknessD = Double.parseDouble(materialThickness);
jointFactorD = Double.parseDouble(jointFactor);

result = this.designPressureD*this.materialDiameterD/ (this.allowableStressD*this.jointFactorD - 0.6*designPressureD);

return result;

} }

我已经通过对代码进行大量更改来处理这种情况: 1- 所有 String.valueOf 已更改为 getText().toString() 并且所有这些都已传输到 onclick 方法 2- Shell.class 已完全更改。一个简单的class就写好了。 3- shell 对象创建已转移到 onclick 方法中。 这是我最后的主要 Activity 代码:

public class MainActivity extends AppCompatActivity {

//Viewportların tanımları
EditText pressure, corrosionAllowance, allowableStress, materialDiameter, materialThickness, jointFactor;
TextView result;
//Objecte aktarılacak variable'lar
String designPressureS, corrosionAllowanceS, allowableStressS, materialDiameterS, materialThicknessS, jointFactorS;
Shell shell;
double designPressureD, corrosionAllowanceD, allowableStressD, materialDiameterD, materialThicknessD, jointFactorD;

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

    //Viewprotlara atıyorum
    pressure = findViewById(R.id.pressure);

    corrosionAllowance = findViewById(R.id.corrosionAllowance);

    allowableStress = findViewById(R.id.allowableStress);

    materialDiameter = findViewById(R.id.materialDiameter);

    materialThickness = findViewById(R.id.materialThickness);

    jointFactor = findViewById(R.id.jointFactor);

    result = findViewById(R.id.result);

}

public void calculation(View view){

    try {

        designPressureS =pressure.getText().toString();
        corrosionAllowanceS = corrosionAllowance.getText().toString();
        allowableStressS = allowableStress.getText().toString();
        materialDiameterS = materialDiameter.getText().toString();
        materialThicknessS = materialThickness.getText().toString();
        jointFactorS = jointFactor.getText().toString();

        designPressureD = Double.parseDouble(designPressureS);
        corrosionAllowanceD = Double.parseDouble(corrosionAllowanceS);
        allowableStressD = Double.parseDouble(allowableStressS);
        materialDiameterD = Double.parseDouble(materialDiameterS);
        materialThicknessD = Double.parseDouble(materialThicknessS);
        jointFactorD = Double.parseDouble(jointFactorS);

        shell = new Shell();

        double resultD = shell.thickness(allowableStressD,materialDiameterD,jointFactorD, designPressureD,corrosionAllowanceD);
        String resultS =String.format("%.2f",resultD);
        result.setText(resultS);
    } catch (Exception e) {
        e.printStackTrace();

    }


}

这是我的决赛shell.class

public class Shell {

public double thickness(double materialSMYS, double outerDiameter, double jointFactor, double pressure, double corrosionAllowance){
return (pressure*outerDiameter/(materialSMYS*jointFactor-0.6*pressure))+corrosionAllowance;}}