Java 我需要帮助错误E/AndroidRuntime:致命异常:主
我的项目有问题。。。。我是在一个试验项目中写的。现在,我把代码移到了主项目中。但是有一些问题。。我该怎么修 BmiMale.javaJava 我需要帮助错误E/AndroidRuntime:致命异常:主,java,android,android-studio,Java,Android,Android Studio,我的项目有问题。。。。我是在一个试验项目中写的。现在,我把代码移到了主项目中。但是有一些问题。。我该怎么修 BmiMale.java package com.example.liveprotect; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppC
package com.example.liveprotect;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class BmiMale extends AppCompatActivity {
private EditText height;
private EditText weight;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_male);
getActionBar().setDisplayHomeAsUpEnabled(true);
height = (EditText) findViewById(R.id.height);
weight = (EditText) findViewById(R.id.weight);
result = (TextView) findViewById(R.id.result);
}
public void mycalculateBMI(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);
}
}
private void displayBMI(float bmi) {
String bmiLabel = "";
if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.very_severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.overweight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.obese_class_i);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.obese_class_ii);
} else {
bmiLabel = getString(R.string.obese_class_iii);
}
bmiLabel = bmi + "\n\n" + bmiLabel;
result.setText(bmiLabel);
}
}
当我单击按钮calculateBMI时
01-04 03:07:44.313 25665-25665/com.example.liveprotect E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.liveprotect, PID: 25665
java.lang.IllegalStateException: Could not find method mycalculateBMI(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'calc'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
不知道为什么你的方法不起作用。您可以采用另一种方法,在
onCreate
方法中显式设置按钮的onClick处理程序:
Button calcButton = (Button)findViewById(R.id.calc);
calcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mycalculateBMI(v);
}
});
尝试清理/重建。关闭仿真器,然后再次尝试运行。我已经运行了,但仍然相同。它仍然存在相同的问题。我不明白为什么,当我将代码移动到MainActivity.java并更改
setContentView(R.layout.bmi_-male)时代码>--->setContentView(R.layout.activity\u main)
和工具:context=“com.example.liveprotect.BmiMale
--->工具:context=“.MainActivity”
我的应用程序正常工作。但每当我切换到“MainActivity”和“activity\u main”以外的其他应用程序时,我的应用程序就会再次出现问题。。。
01-04 03:07:44.313 25665-25665/com.example.liveprotect E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.liveprotect, PID: 25665
java.lang.IllegalStateException: Could not find method mycalculateBMI(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'calc'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Button calcButton = (Button)findViewById(R.id.calc);
calcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mycalculateBMI(v);
}
});