计算器应用程序上的吐司错误

时间:2014-11-07 15:38:54

标签: java android calculator toast

我正在努力奋斗以克服空的editText问题。 没有语法错误,但应用程序意外停止。

我不确定问题是来自侦听器还是IF循环。但是除了空的editText异常之外,计算器工作正常。

谢谢!

这是我的XML:

<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.rayana.calculator.MainActivity" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="39dp"
        android:text="Result ="
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignLeft="@+id/button1"
        android:layout_marginBottom="31dp"
        android:text="B =" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignLeft="@+id/textView2"
        android:text="A =" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_marginBottom="45dp"
        android:ems="10"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/button2"
        android:ems="10"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:text="-" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:text="+" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/button2"
        android:text="x" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/button3"
        android:text="÷" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView3"
        android:ems="10"
        android:inputType="numberDecimal" />

</RelativeLayout>

My Main.java:

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

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



    Button plus = (Button)findViewById(R.id.button1);
    Button minus = (Button)findViewById(R.id.button2);
    Button multiply = (Button)findViewById(R.id.button3);
    Button devide = (Button)findViewById(R.id.button4);




    plus.setOnClickListener(Operation1);
    minus.setOnClickListener(Operation1);
    multiply.setOnClickListener(Operation1);
    devide.setOnClickListener(Operation1);

 }
         ///Your click listener method:
 OnClickListener Operation1 = new OnClickListener() {

        public void onClick(View v) {

            final EditText editText1 = (EditText)findViewById(R.id.editText1);

            final EditText editText2 = (EditText)findViewById(R.id.editText2);

            final EditText editText3 = (EditText)findViewById(R.id.editText3);          

            if(editText1.getText().equals("") || editText2.getText().equals(""))  
            {
                 //toast the error message
                Context context = getApplicationContext();
                CharSequence text = "Please enter the numbers !";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                 return;
            }
            else
            {
                final double A = Double.valueOf(editText1.getText().toString());
                final double B = Double.valueOf(editText2.getText().toString());
                switch(v.getId()){

                case R.id.button1:

                    double result = A+B;
                    editText3.setText(String.valueOf(result));
                    break;
                case R.id.button2:
                    double result2 = A-B;
                    editText3.setText(String.valueOf(result2));
                    break;
                case R.id.button3:
                    double result3 = A*B;
                    editText3.setText(String.valueOf(result3));
                    break;
                case R.id.button4:
                    double result4 = A/B;
                    editText3.setText(String.valueOf(result4));
                    break;
                }
            }

            }
    };


}

和我的Logcat:

11-07 15:32:43.518: V/TLINE(364): new: android.text.TextLine@40685a60
11-07 15:32:43.877: V/TLINE(364): new: android.text.TextLine@406920b0
11-07 15:32:45.847: D/AndroidRuntime(364): Shutting down VM
11-07 15:32:45.847: W/dalvikvm(364): threadid=1: thread exiting with uncaught exception (group=0x40014760)
11-07 15:32:45.887: E/AndroidRuntime(364): FATAL EXCEPTION: main
11-07 15:32:45.887: E/AndroidRuntime(364): java.lang.NumberFormatException: Invalid double: ""
11-07 15:32:45.887: E/AndroidRuntime(364):  at org.apache.harmony.luni.util.FloatingPointParser.invalidReal(FloatingPointParser.java:78)
11-07 15:32:45.887: E/AndroidRuntime(364):  at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:276)
11-07 15:32:45.887: E/AndroidRuntime(364):  at java.lang.Double.parseDouble(Double.java:317)
11-07 15:32:45.887: E/AndroidRuntime(364):  at java.lang.Double.valueOf(Double.java:354)
11-07 15:32:45.887: E/AndroidRuntime(364):  at com.rayana.calculator2.MainActivity$1.onClick(MainActivity.java:62)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.view.View.performClick(View.java:3100)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.view.View$PerformClick.run(View.java:11644)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.os.Handler.handleCallback(Handler.java:587)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.os.Looper.loop(Looper.java:126)
11-07 15:32:45.887: E/AndroidRuntime(364):  at android.app.ActivityThread.main(ActivityThread.java:3997)
11-07 15:32:45.887: E/AndroidRuntime(364):  at java.lang.reflect.Method.invokeNative(Native Method)
11-07 15:32:45.887: E/AndroidRuntime(364):  at java.lang.reflect.Method.invoke(Method.java:491)
11-07 15:32:45.887: E/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-07 15:32:45.887: E/AndroidRuntime(364):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-07 15:32:45.887: E/AndroidRuntime(364):  at dalvik.system.NativeStart.main(Native Method)
11-07 15:32:46.017: D/dalvikvm(364): GC_CONCURRENT freed 193K, 5% free 6460K/6791K, paused 4ms+12ms

3 个答案:

答案 0 :(得分:1)

问题是editText1或editText2中的值不是有效的double值。

你应该围绕这些线:

            final double A = Double.valueOf(editText1.getText().toString());
            final double B = Double.valueOf(editText2.getText().toString());

使用try / catch块,例如:

try{
     final double A = Double.valueOf(editText1.getText().toString());
    final double B = Double.valueOf(editText2.getText().toString());
}catch(NumberFormatException e){
    //invalid double value
}

答案 1 :(得分:1)

将您的代码更改为:

if(editText1.getText().toString().equals("") || editText2.getText().toString().equals("")) 

我猜您忘记添加toString,因此equals方法会给出错误的结果,因此您的代码会转到else语句和{ {1}}发生在那里(因为“”一个NumberFormatException)。

答案 2 :(得分:0)

您的错误发生在com.rayana.calculator2.MainActivity$1.onClick(MainActivity.java:62)

其中包含以下命令double result = A+B;

没有你要找的双打