我创建了这个,但函数一直有错误?

时间:2016-08-24 14:07:22

标签: java android debugging nullpointerexception

基本上,我正试图根据他们的BMI让这个吃健康的应用程序。计算器已经完成,但现在我陷入困境,用户点击这3个按钮中的一个,Spinner项中的列表和ImageView相应地更改(如果用户点击早餐按钮,那么微调器列表和图像更改为早餐等)。即使图像不起作用。该应用程序运行,但当我点击按钮它终止,我不知道如何解决它。如果有可能的话,你们能不能让我知道我正在做的事Spinner会不会起作用?

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button Toch;
    Button BTbtn;
    Button LunchBtn;
    Button DinnerBtn;
    Button Submit;
    EditText inweight;
    EditText inheight;
    EditText inage;
    TextView BMR;
    RadioButton rdM;
    RadioButton rdF;
    ImageView maimage;
    Spinner spinner;

    /**
        Button Submit = (Butt
                * ATTENTION: This was auto-generated to implement the App Indexing API.
        * See https://g.co/AppIndexing/AndroidStudio for more information.
        */
        private GoogleApiClient client;

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

            final Button Submit = (Button)findViewById(R.id.Sumbit);
            final Button Toch = (Button) findViewById(R.id.Toch);
            Toch.setOnClickListener(this);
            final Button BTbtn = (Button) findViewById(R.id.BTbtn);
            BTbtn.setOnClickListener(this);
            final Button LunchBtn = (Button) findViewById(R.id.LunchBtn);
            LunchBtn.setOnClickListener(this);
            final Button DinnerBtn = (Button) findViewById(R.id.DinnerBtn);
            DinnerBtn.setOnClickListener(this);
            //EditText
            final EditText inweight = (EditText) findViewById(R.id.inweight);
            final EditText inheight = (EditText) findViewById(R.id.inheight);
            final EditText inage = (EditText) findViewById(R.id.inage);

            //Text View
            final TextView BMR = (TextView) findViewById(R.id.BMR);
            // final TextView FDName=(TextView)findViewById(R.id.FDName);
            //RadioButton
            final RadioButton rdM = (RadioButton) findViewById(R.id.rdM);
            final RadioButton rdF = (RadioButton) findViewById(R.id.rdF);

            //ImageView
            final ImageView maimage = (ImageView) findViewById(R.id.maimage);


        }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.BTbtn: {
                maimage.setImageResource(R.drawable.breakfast);
                //ArrayAdapter bList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.breakfastList, android.R.layout.simple_dropdown_item_1line);
            }

            case R.id.LunchBtn: {
                maimage.setImageResource(R.drawable.dinner);
                //ArrayAdapter lList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.lunchList, android.R.layout.simple_dropdown_item_1line);
            }

            case R.id.DinnerBtn: {
                maimage.setImageResource(R.drawable.lunch);
                //ArrayAdapter dList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.dinnerList, android.R.layout.simple_dropdown_item_1line);
            }

            case R.id.Sumbit: {
                double weight = Double.parseDouble(inweight.getText().toString());
                double height = Double.parseDouble(inheight.getText().toString());
                double age = Double.parseDouble(inage.getText().toString());

                double gender;

                if (rdM.isChecked()) {
                    gender = 66;
                    double ans = gender + (13.7 * weight) + (5 * height) - (6.8 * age);
                    BMR.setText("" + (int) ans);
                }
                if (rdF.isChecked()) {
                    gender = 655;
                    double ans = gender + (9.6 * weight) + (1.8 * height) - (4.7 * age);
                    BMR.setText("Your Calories require : " + (int) ans);
                }
            }

            case R.id.Toch: {
                inweight.setHint(R.string.chweight);
                inheight.setHint(R.string.chhight);
                inage.setHint(R.string.chage);
            }
        }
    }
}

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cmleu_000.myapplication.MainActivity">

    <TextView
        android:text="Food Name:"
        android:textSize="18dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:id="@+id/BTbtn"
        android:text="Breakfast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/LunchBtn"
        android:layout_alignRight="@+id/radioGroup2"
        android:layout_alignEnd="@+id/radioGroup2" />

    <Button
        android:id="@+id/LunchBtn"
        android:text="Lunch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/DinnerBtn"
        android:layout_alignRight="@+id/BTbtn"
        android:layout_alignEnd="@+id/BTbtn" />


    <Button
        android:id="@+id/DinnerBtn"
        android:text="Dinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/maimage"
        android:layout_alignLeft="@+id/LunchBtn"
        android:layout_alignStart="@+id/LunchBtn" />


    <ImageView
        android:id="@+id/maimage"
        android:src="@drawable/dinner"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:layout_above="@+id/qweight"
        android:layout_alignLeft="@+id/qhight"
        android:layout_alignStart="@+id/qhight" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Sumbit"
        android:layout_alignRight="@+id/Sumbit"
        android:layout_alignEnd="@+id/Sumbit"
        android:id="@+id/radioGroup2">

        <RadioButton
            android:id="@+id/rdF"
            android:text="F"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/rdM"
            android:layout_toRightOf="@+id/rdM"
            android:layout_toEndOf="@+id/rdM"
            android:layout_gravity="left" />

        <RadioButton
            android:id="@+id/rdM"
            android:text="M"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/radioGroup"
            android:layout_toRightOf="@+id/qage"
            android:layout_toEndOf="@+id/qage"
            android:layout_gravity="left" />
    </RadioGroup>

    <TextView
        android:text="Your weight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/qweight"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <EditText
        android:id="@+id/inweight"
        android:hint="Plase input your weight (KG)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/qhight"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="Your height"
        android:id="@+id/qhight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/inheight"
        android:layout_alignRight="@+id/qweight"
        android:layout_alignEnd="@+id/qweight" />

    <EditText
        android:id="@+id/inheight"
        android:hint="Please input your height (CM)"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/qage"
        android:layout_alignRight="@+id/inweight"
        android:layout_alignEnd="@+id/inweight" />

    <TextView
        android:text="Your age"
        android:id="@+id/qage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/inage"
        android:layout_alignLeft="@+id/BMR"
        android:layout_alignStart="@+id/BMR" />

    <EditText
        android:id="@+id/inage"
        android:hint="Please input your age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Sumbit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/Sumbit"
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:text="Your BMR"
        android:id="@+id/BMR"
        android:textStyle="bold"
        android:textSize="10pt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/inheight"
        android:layout_alignStart="@+id/inheight" />

    <Button
        android:text="中文"
        android:id="@+id/Toch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/BMR"
        android:layout_toLeftOf="@+id/Sumbit"
        android:layout_toStartOf="@+id/Sumbit" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_above="@+id/BTbtn"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

logcat的:

08-24 22:43:52.039 3104-3104/com.example.cmleu_000.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.cmleu_000.myapplication, PID: 3104
                                                                                   java.lang.NumberFormatException: Invalid double: ""
                                                                                       at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                                       at java.lang.StringToReal.parseDouble(StringToReal.java:267)
                                                                                       at java.lang.Double.parseDouble(Double.java:301)
                                                                                       at com.example.cmleu_000.myapplication.MainActivity.onClick(MainActivity.java:92)
                                                                                       at android.view.View.performClick(View.java:5198)
                                                                                       at android.view.View$PerformClick.run(View.java:21147)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-24 22:43:53.601 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 8.662ms
08-24 22:44:10.123 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 5.787ms

*编辑后的新错误

1 个答案:

答案 0 :(得分:2)

您已定义maimage变量两次。您尝试在maimage方法中访问的类变量onClick(...)null(它从未初始化)。

要解决此问题,请更改:

final ImageView maimage = (ImageView) findViewById(R.id.maimage);

为:

maimage = (ImageView) findViewById(R.id.maimage);

这样,您将初始化类变量,而不是定义具有相同名称的新变量。

您在onCreate(...)方法中再次定义的所有其他变量也是如此。