致命错误:主要错误-radio按钮

时间:2014-08-02 19:53:17

标签: java android exception radio-button fatal-error

我正在尝试编写android减肥计划器。在第一部分中,我尝试计算BMI和理想体重以及每日卡路里。我在genymotion中运行项目,当我点击单选按钮更改性别时,会出现致命错误。我是Android开发的新手,所以我不太了解它。

任何人都可以帮助我吗?

错误

08-02 19:30:44.807: E/AndroidRuntime(883): FATAL EXCEPTION: main
08-02 19:30:44.807: E/AndroidRuntime(883): java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class com.dietandroidproject.MainActivity for onClick handler on view class android.widget.RadioButton with id 'maleselected'
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.view.View$1.onClick(View.java:3578)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.view.View.performClick(View.java:4084)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.widget.CompoundButton.performClick(CompoundButton.java:100)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.view.View$PerformClick.run(View.java:16966)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.os.Handler.handleCallback(Handler.java:615)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.os.Looper.loop(Looper.java:137)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-02 19:30:44.807: E/AndroidRuntime(883):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 19:30:44.807: E/AndroidRuntime(883):  at java.lang.reflect.Method.invoke(Method.java:511)
08-02 19:30:44.807: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-02 19:30:44.807: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-02 19:30:44.807: E/AndroidRuntime(883):  at dalvik.system.NativeStart.main(Native Method)
08-02 19:30:44.807: E/AndroidRuntime(883): Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
08-02 19:30:44.807: E/AndroidRuntime(883):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-02 19:30:44.807: E/AndroidRuntime(883):  at java.lang.Class.getMethod(Class.java:915)
08-02 19:30:44.807: E/AndroidRuntime(883):  at android.view.View$1.onClick(View.java:3571)
08-02 19:30:44.807: E/AndroidRuntime(883):  ... 12 more

主要活动代码

package com.dietandroidproject;

import Databasedata.Person;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

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


    Button saveinformation = (Button) findViewById(R.id.saveinformation);
    saveinformation.setOnClickListener(new View.OnClickListener() {

        EditText weighttext = (EditText) findViewById(R.id.weighttext);
        EditText heighttext = (EditText) findViewById(R.id.heighttext);
        EditText usernametext = (EditText) findViewById(R.id.usernametext);
        EditText agetext = (EditText) findViewById(R.id.agetext);
        RadioGroup genderselected = (RadioGroup) findViewById(R.id.selectgender);
        Spinner activitytext = (Spinner) findViewById(R.id.chooseactivity);
        Button saveinformation = (Button) findViewById(R.id.saveinformation);
        TextView genderchoosed = (TextView) findViewById(genderselected
                .getCheckedRadioButtonId());
        String gender = genderchoosed.getText().toString();
        String pa = activitytext.getSelectedItem().toString();

        @Override
        public void onClick(View v) {

            int weight = (int) Float.parseFloat(weighttext.getText()
                    .toString());
            int height = (int) Float.parseFloat(heighttext.getText()
                    .toString());
            String username = usernametext.getText().toString();
            int age = (int) Float.parseFloat(agetext.getText().toString());
            TextView genderchoosed = (TextView) findViewById(genderselected
                    .getCheckedRadioButtonId());
            String gender = genderchoosed.getText().toString();
            String pa = activitytext.getSelectedItem().toString();
            //=========================================================
            int Bmivalue = calculateBMI(weight, height);
            String bmiInterpretation = interpretBMI(Bmivalue);
            float idealweight = idealweight(weight, height, gender, pa, age);
            double dailycalories=dailycalories(weight,height,gender,pa,age);
             // insert in to============================================

            Person person = new Person();
            person.setUsername(username);
            person.setHeight(height);
            person.setWeight(weight);
            person.setAge(age);
            person.setGender(gender);
            person.setPa(pa);
            person.setBmivalue(Bmivalue);
            person.setBmiInterpretation(bmiInterpretation);
            person.setIdealweight(idealweight);
            person.setDailycalories(dailycalories);
            Databasedata.DatabaseAdapter dbAdapter = new Databasedata.DatabaseAdapter(
                MainActivity.this);
            dbAdapter.insertPerson(person);

            Toast.makeText(getApplicationContext(),
                Bmivalue + "and you are" + bmiInterpretation,
                Toast.LENGTH_LONG).show();

               }
             });

             }
             // BMI function==================================================
             private int calculateBMI(int weight, int height) {

               return (int) (weight / (height * height));
             };

             private String interpretBMI(int Bmivalue) {

                 if (Bmivalue < 18.5) {
                   return "Underweight";
                 } else if (Bmivalue < 25) {
                   return "Normal";
                 } else if (Bmivalue < 30) {
                   return "Overweight";
                 } else {
                   return "Obese";
                  }

                  }
                //==========================IDEAL WEIGHT function======================
                 private float idealweight(int weight, int height, String gender, String pa,
    int age) {
                   float x = (float) 0.0;
                   int bmi;

                   bmi = idealbmi(age);
                  x = bmi * (height * height);
                  return x;

                    }

                  public int idealbmi(int age) {
                  int bmi = 0;
                   if (age > 17 && age <= 19) {
                    bmi = 21;
                    }
                   if (age > 19 && age <= 24) {
                    bmi = 22;
                    }
                   if (age > 24 && age <= 34) {
                    bmi = 23;
                    }
                   if (age > 34 && age <= 44) {
                    bmi = 24;
                    }
                   if (age > 44 && age <= 54) {
                    bmi = 25;
                    }
                  if (age > 54 && age <= 64) {
                    bmi = 26;
                   }
                  if (age > 64) {
                    bmi = 27;
                   }

                  return bmi;

                 =====================DAILY CALORIES function ==============
               public double suitablepa(String pa) {
               double suitablepa = 0;
               if (pa.equals("Highly active")) {
               suitablepa =  1.48;
               }
               if (pa.equals("Active")) {
                suitablepa =  1.25;
               }
               if (pa.equals("Low activity")) {
                suitablepa =  1.11;
               }
               if (pa.equals("Sedentary")) {
                suitablepa =  1;
               }
                return suitablepa;

               }
  private double dailycalories(int weight, int height, String gender,
    String pa, int age) {
    double dailycalori = 0;
    double i;
    double j;
    double h;
    double k;
   if(gender.equals("femaleselected")){

    i=9.36*weight;
    j=726*height;
    h=suitablepa(pa)*(i+j);
    k=6.91*age;
    dailycalori=354-k+h;

}else if(gender.equals("maleselected")){

    i=15.91*weight;
    j=539.6*height;
    h=suitablepa(pa)*(i+j);
    k=9.53*age;
    dailycalori=662-k+h;
}
return dailycalori;

XML CODES

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgroundmain"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/personinformation"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.98" >

    <EditText
        android:id="@+id/heighttext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/usernametext"
        android:layout_below="@+id/usernametext"
        android:ems="10"
        android:hint="Enter Your Height" >
    </EditText>

    <EditText
        android:id="@+id/usernametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter Username" />

    <EditText
        android:id="@+id/weighttext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/heighttext"
        android:layout_below="@+id/heighttext"
        android:ems="10"
        android:hint="Enter Your Weight" />

    <EditText
        android:id="@+id/agetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/weighttext"
        android:layout_below="@+id/weighttext"
        android:ems="10"
        android:hint="Enter Your Age" >

        <requestFocus />
    </EditText>

</RelativeLayout>

<View
    android:layout_width="250dp"
    android:layout_height="1dip"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="#aaa" />

<RelativeLayout
    android:id="@+id/choosegender"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.78" >

    <TextView
        android:id="@+id/choosefemaleormale"
        android:layout_width="match_parent"
        android:layout_height="30dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dip"
        android:gravity="center"
        android:text="Gender : "
        android:textAlignment="center"
        android:textColor="#555"
        android:textSize="19sp" />

    <RadioGroup
        android:id="@+id/selectgender"
        android:layout_width="220dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/choosefemaleormale"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/femaleselected"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:checked="true"
            android:text="female"
            android:onClick="onRadioButtonClicked"
             />

        <RadioButton
            android:id="@+id/maleselected"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="male"
            android:onClick="onRadioButtonClicked" />
    </RadioGroup>
</RelativeLayout>
    <View
    android:layout_width="250dp"
    android:layout_height="1dip"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="#aaa" />
<RelativeLayout
    android:id="@+id/choosepa"
    android:layout_width="250dip"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_gravity="center" >

    <Spinner
        android:id="@+id/chooseactivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/activityitems"
        android:gravity="center"
        android:prompt="@string/level_of_activity" />

</RelativeLayout>

<Button
    android:layout_width="90dp"
    android:layout_height="0dp"
    android:layout_gravity="right"
    android:layout_marginBottom="10dip"
    android:layout_marginRight="20dp"
    android:layout_weight="0.46"
    android:background="@drawable/recent_foods_depressed"
    android:hint="save"
    android:text="save"
    android:textColor="#fff"
    android:textSize="20sp"
    android:textStyle="bold" 
    android:onClick="saveinformation"
    android:id="@+id/saveinformation"/>

    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

问题很简单:您编写了andorid:onClick="onRadioButtonClick,这意味着当单击按钮时,应调用Activity中的方法onRadioButtonClicked,但它不存在。只需将onRadioButtonClicked方法添加到您的Activity中,它就可以正常工作。

答案 1 :(得分:0)

在你的xml文件中添加:

<RadioGroup
    android:id="@+id/radiogender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

<TextView
    android:id="@+id/gender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="gender" />

并在java中添加:

final RadioGroup radioSexGroup = (RadioGroup) findViewById(R.id.radiogender);

    final TextView m = (TextView) findViewById(R.id.gender);

    radioSexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int selectedId) {
             selectedId = radioSexGroup.getCheckedRadioButtonId();

            RadioButton radioSexButton = (RadioButton) findViewById(selectedId);
            m.setText(radioSexButton.getText());

        }
    });