【问题标题】:java.lang.IllegalStateException: Could not find method in a parent or ancestor Contextjava.lang.IllegalStateException:在父或祖先上下文中找不到方法
【发布时间】:2019-06-22 15:15:55
【问题描述】:

我正在尝试为我的家庭作业制作一个应用程序,我发现这个 BMI 计算器非常符合我的想法。所以我试图将它添加到我的项目中,但是当我按下按钮计算BMI时,我收到一个错误消息:

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)

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

计算BMI.java:

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);
    }}

calculate_bmi.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/calculate_bmi"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.assignment.asistentdigital.entity.CalculateBMI">

    <EditText
        android:id="@+id/weight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:ems="6"
        android:inputType="number|numberDecimal"
        android:textSize="14sp"/>

    <EditText
        android:id="@+id/height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:ems="6"
        android:inputType="number|numberDecimal"
        android:textSize="14sp"/>

    <Button
        android:id="@+id/calc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:onClick="calculateBMI"
        android:text="calculeaza"/>

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:textSize="20sp"/>

</LinearLayout> 

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

【问题讨论】:

  • “但是我为我的计算器创建了一个新类,称为“CalculateBMI”,因为我已经在使用“MainActivity””——你显示的是哪个活动?通常,MainActivity 类使用名为 activity_main 的布局。您似乎也在 CalculateBMI 课程中使用它。如果是这种情况——您在两个活动中都使用此布局,那么当您单击MainActivity 中的calc 按钮时,您将因此错误而崩溃,因为MainActivity(可能)没有@987654331 @方法。
  • 好的,现在我明白了为什么我的程序会混淆了,但是我创建了一个新的 XML 文件并将其命名为 calculate_bmi,我确定我更改了 setContentView,点击时我也遇到了同样的错误:(
  • 正如我最初问的,你展示的是哪个活动?
  • 很抱歉,我错过了这个问题。我正在使用CalculateBMI,我没有在其中混合MainActivity。另外,我是新手,所以我可能会误解一些事情,我很抱歉
  • 此时,您可能应该更新您的问题。显示您当前的代码、您当前的布局,并提供完整的堆栈跟踪(不仅仅是第一行,就像现在一样)。

标签: java android


【解决方案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);
    }
}

它会正常工作;)

【讨论】:

  • 问题是关于使用 xml 实现这一点。
  • 您能详细说明问题吗
  • @AliAmini 我必须构建一个关于健康的应用程序,我想将此计算器添加到我的应用程序中,我按照教程进行操作,但它不起作用。我所做的一切都以这个错误告终!!!截止日期是 7 月 2 日,这个计算器本来可以让我取得好成绩,所以我真的很想让它工作,但似乎没有什么对我有用。
  • github.com/iasjem/bmi-calculator-android这是一个简单的BMI计算来源尝试自定义它希望对您有所帮助
【解决方案2】:

我尝试了你的代码,它对我来说工作正常,我认为你正在做的可能的错误是,你已经将 calculate_bmi 的布局文件包含在 activity_main xml 中,并在 MainActivity 中使用了这个 xml,但 MainActivity 仍然被定义为启动器活动在你的主祭中。

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

所以请确保您正在做以下 2 件事之一以实现相同的目标。

  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>
    

  1. 在 MainActivity 中定义 CalculateBMI 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多