学习Android Java - 应用程序失败

我一直在努力学习Java for Android开发,所以我决定尝试制作一个简单的转换器应用程序来学习 . 目前我有一个简单的用户界面,我正在尝试从摄氏温度转换为华氏温度 . 转换器在工作时会转换为Celsius,Fahrenheit和Kelvin .

当我单击应该运行计算方法的按钮时,我收到错误“不幸的是,转换器已停止” . 下面是我的代码,我也为视图包含了XML .

package com.michaelmurphy.converter;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.EditText;
    import android.widget.Spinner;

    public class Temperature extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.temperature_view);

            // TODO Auto-generated method stub
        }

        public void tempCalc()
        {
            //define variables
            float value = 0;
            String from = "";//for spinner
            String to = "";//for spinner

            //get entered value
            EditText input = (EditText) findViewById(R.id.editText1);
            //convert to string
            String enteredValue = input.getText().toString();
            //convert string into float
            float num = Float.valueOf(enteredValue);

            //retrieve the from spinner value
            final Spinner fromSpinner = (Spinner) findViewById(R.id.spinner1);
            from = fromSpinner.getSelectedItem().toString();    
            //retrieve the to spinner value
            final Spinner toSpinner = (Spinner) findViewById(R.id.spinner1);
            to = toSpinner.getSelectedItem().toString();

            EditText output = (EditText) findViewById(R.id.textView2);

            /*if(from.equals(to)) //the same conversion type
            {
                //error
            }*/

            if(from.equals("Celcius"))
            {
                if(to.equals("Fahrenheit"))
                {
                    value = celToFar(num);
                } 
                else 
                {
                    //value = celToKel(num);
                }
            }
            else if(from.equals("Fahrenheit"))
            {
                if(to.equals("Celcius"))
                {
                    //value = fahToCel(num);
                } 
                else 
                {
                    //value = fahToKel(num);
                }
            }
            else //kelvin
            {
                if(to.equals("Celcius"))
                {
                    //value = kelToCel(num);
                }
                else 
                {
                    //value = kelToFah(num);
                }
            }

            //set the label to variable value
            String valueStr = Float.toString(value);//cast float to string
            output.setText(valueStr);
        }

        public float celToFar(float cel)
        {
            float fah = cel * 9/5 + 32;
            return fah;
        }

    }

查看XML代码:

``
`<?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:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/temp_arr" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/temp_arr" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/convertBtn"
    android:onClick="tempCalc" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>`
``

有人能够指出我哪里出错了,我不知道 . 谢谢

回答(1)

3 years ago

更改:

public void tempCalc()

public void tempCalc(View v)

任何onClick方法都需要 View 参数 . 因为你没有_1352442匹配,你的应用程序会抛出异常 .