java.lang.IllegalStateException:在父级或祖先上下文中找不到方法

时间:2019-06-22 15:15:55

标签: java android

我正在尝试为我的家庭作业创建一个应用程序,我发现这个BMI计算器确实符合我的想法。因此,我试图将其添加到我的项目中,但是当我按下按钮来计算BMI时,我收到一条错误消息:

div.listcontent.clear

我在这里https://www.ssaurel.com/blog/learn-to-create-a-bmi-calculator-app-for-android/遵循了本教程 但是我为计算器创建了一个新类,称为“ CalculateBMI”,因为我已经在使用“ MainActivity”

CalculateBMI.java:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.assignment.careys, PID: 5827
    java.lang.IllegalStateException: Could not find method calculateBMI(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'calc'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

calculate_bmi.xml:

public class CalculateBMI extends AppCompatActivity {

    private EditText height;
    private EditText weight;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate_bmi);
        height = (EditText) findViewById(R.id.height);
        weight = (EditText) findViewById(R.id.weight);
        result = (TextView) findViewById(R.id.result);
    }

    public void calculateBMI(View v) {
        String heightStr = height.getText().toString();
        String weightStr = weight.getText().toString();

        if (heightStr != null && !"".equals(heightStr)
                && weightStr != null  &&  !"".equals(weightStr)) {
            float heightValue = Float.parseFloat(heightStr) / 100;
            float weightValue = Float.parseFloat(weightStr);

            float bmi = weightValue / (heightValue * heightValue);

            displayBMI(bmi);
        }
    }

    public void displayBMI(float bmi) {
        String bmiLabel = "";

        if (Float.compare(bmi, 15f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 15f) > 0  &&  Float.compare(bmi, 16f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 16f) > 0  &&  Float.compare(bmi, 18.5f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 18.5f) > 0  &&  Float.compare(bmi, 25f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 25f) > 0  &&  Float.compare(bmi, 30f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 30f) > 0  &&  Float.compare(bmi, 35f) <= 0) {
            bmiLabel = "";
        } else if (Float.compare(bmi, 35f) > 0  &&  Float.compare(bmi, 40f) <= 0) {
            bmiLabel = "";
        } else {
            bmiLabel = "";
        }

        bmiLabel = bmi + "\n\n" + bmiLabel;
        result.setText(bmiLabel);
    }}

我想将此XML包含在另一个XML中,并计算并显示体重指数。问题是每次我按下按钮进行计算时,我的应用程序崩溃,并给我错误

2 个答案:

答案 0 :(得分:1)

1:在CalculateBMI.java中更改更改setContentView(R.layout.activity_main);至 setContentView(R.layout.calculate_bmi);

2:在CalculateBMI.java中为calc按钮设置点击监听器

private Button CalcBtn;
.
.
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    height = (EditText) findViewById(R.id.height);
    weight = (EditText) findViewById(R.id.weight);
    result = (TextView) findViewById(R.id.result);
    CalcBtn = (Button) findViewById(R.id.calc);
    CalcBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateBMI();
            }
        });
}

将calculateBMI方法更改为:

private void calculateBMI() {
    String heightStr = height.getText().toString();
    String weightStr = weight.getText().toString();
    if (heightStr != null && !"".equals(heightStr)
            && weightStr != null  &&  !"".equals(weightStr)) {
        float heightValue = Float.parseFloat(heightStr) / 100;
        float weightValue = Float.parseFloat(weightStr);
        float bmi = weightValue / (heightValue * heightValue);
        displayBMI(bmi);
    }
}

它将正常工作;)

答案 1 :(得分:0)

我尝试了您的代码,对我来说很好用,我认为您正在做的可能错误是,您已将calculate_bmi的布局文件包含在activity_main xml中,并在MainActivity中使用了该xml,但MainActivity仍被定义为启动器活动在您的主要节日中。

因此,当您启动应用程序时,Main活动会显示您的布局,但是当您单击按钮时,它不会在MainActivity类中找到该方法。

因此,请确保您正在执行以下两项操作之一以实现相同的目的。

  1. 将其更改为您的AndroidManifest.xml

       <activity android:name=".MainActivity">
    
        </activity>
    
        <activity android:name=".CalculateBMI">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

OR

  1. 将CalculateBMI方法定义为MainActivity。