仅当输入为空时,应用程序崩溃

时间:2018-08-09 05:02:11

标签: android

需要帮助的原因,如果我单击:对空字符串/输入进行处理计算,则我的应用程序总是崩溃。但没有价值的问题:

我已经尝试了多种方法来拦截空白的editText,即:如果ALTER TABLE tbl3 ADD UNIQUE KEY (field_from_tbl1, field_from_tbl2) isEmptyequal(""),包括尝试并捕获但没有成功

这是我的最后一个代码:

== null

}

以下是崩溃日志:

import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;

public class EngineActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button switchButton = (Button) findViewById(R.id.switchButton);

    //Listening to button event
    switchButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        //Starting a new Intent
        Intent nextScreen = new Intent(getApplicationContext(), EngineSecondActivity.class);
        startActivity(nextScreen);

    }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.engine, menu);
    return true;
}

public void calculateClickHandler(View view) {
    // get the references to the widgets
    EditText antText = (EditText)findViewById(R.id.antText);
    EditText degText = (EditText)findViewById(R.id.degText);
    EditText spanText = (EditText)findViewById(R.id.spanText);
    TextView resultText1 = (TextView)findViewById(R.id.resultLabel1);
    TextView resultText2 = (TextView)findViewById(R.id.resultLabel2);
    TextView resultText3 = (TextView)findViewById(R.id.resultLabel3);
    String ant = antText.getText().toString();
    String deg = degText.getText().toString();
    String span = spanText.getText().toString();

    // make sure we handle the click of the calculator button
    if (view.getId() == R.id.calculateButton) {

     // get the users values from the widget references         
        if (ant.isEmpty() || deg.isEmpty() || span.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
            return;

        } else {
         float h = Float.parseFloat(ant); 
         float t = Float.parseFloat(deg);
         float b = Float.parseFloat(span);

     // calculate disc value
         float disc1 = calculateDisc1(h, t, b);
         float disc2 = calculateDisc2(h, t);
         float disc3 = calculateDisc3(h, t, b);

     // now set the value in the result text
         resultText1.setText("d1: " + disc1 + " m");
         resultText2.setText("d2: " + disc2 + " m");
         resultText3.setText("d3: " + disc3 + " m");
     }
    }
}

   // the formula to calculate 

   private float calculateDisc1 (float h, float t, float b) {

    return (float) (h / Math.abs(Math.tan((t + b / 2) * Math.PI/180)));
   }

   private float calculateDisc2 (float h, float t) {

    return (float) (h / Math.abs(Math.tan(t * Math.PI/180)));
   }

   private float calculateDisc3 (float h, float t, float b) {

    return (float) (h / Math.abs(Math.tan((t - b / 2) * Math.PI/180)));
   }

随此是布局xml:

java.lang.IllegalStateException:     at 
    android.view.View$DeclaredOnClickListener.onClick (View.java:5385)    at  
    android.view.View.performClick (View.java:6305)    at
    android.view.View$PerformClick.run (View.java:24840)    at
    android.os.Handler.handleCallback (Handler.java:790)    at
    android.os.Handler.dispatchMessage (Handler.java:99)    at
    android.os.Looper.loop (Looper.java:164)    at
    android.app.ActivityThread.main (ActivityThread.java:6501)    at
    java.lang.reflect.Method.invoke (Native Method)    at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)    at
    com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

Caused by: 
    java.lang.reflect.InvocationTargetException:     at
    java.lang.reflect.Method.invoke (Native Method)    at
    android.view.View$DeclaredOnClickListener.onClick (View.java:5380)

Caused by: 
    java.lang.NumberFormatException:     at
    sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal.java:1842)    at
    sun.misc.FloatingDecimal.parseFloat (FloatingDecimal.java:122)    at 
    java.lang.Float.parseFloat (Float.java:452)    at 
    com.xxx.yyy.EngineActivity.calculateClickHandler (EngineActivity.java:55)

7 个答案:

答案 0 :(得分:1)

NumberFormatException当给定的字符串不是指定格式时发生。

Float.parseFloat()如果指定的参数不是浮点格式,则抛出NumberFormatException。

例如,

13->它是一个整数,不是浮点型。

13.0->这实际上是浮点格式。

如果您尝试解析空字符串中的float,也会发生这种情况。

答案 1 :(得分:0)

从edittext读取文本时的修剪值。

String ant = antText.getText().toString().trim();
String deg = degText.getText().toString().trim();
String span = spanText.getText().toString().trim();

尝试使用TextUtils.isEmpty()检查空输入,如下所示。

 if (TextUtils.isEmpty(ant)) {
            Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(deg)) {
            Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(span)) {
            Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
            return;
        }
        float h = Float.parseFloat(ant);
        float t = Float.parseFloat(deg);
        float b = Float.parseFloat(span);

        // calculate disc value
        float disc1 = calculateDisc1(h, t, b);
        float disc2 = calculateDisc2(h, t);
        float disc3 = calculateDisc3(h, t, b);

        // now set the value in the result text
        resultText1.setText("d1: " + disc1 + " m");
        resultText2.setText("d2: " + disc2 + " m");
        resultText3.setText("d3: " + disc3 + " m");

希望有帮助。

答案 2 :(得分:0)

尝试一下

float h = Float.valueOf(ant); 
float t = Float.valueOf(deg);
float b = Float.valueOf(span);

OR

float h = Float.valueOf(antText.getText().toString()); 
float t = Float.valueOf(degText.getText().toString());
float b = Float.valueOf(spanText.getText().toString());

答案 3 :(得分:0)

无论您插入十进制值,整数值还是空值,我都对您的代码进行了一些小的更改,并且对我的工作正常。没有面临任何崩溃。

 import android.os.Bundle;
    import android.content.Intent;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;

    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.Button;

        public class EngineActivity extends Activity {

            EditText antText, degText, spanText;
            TextView resultText1, resultText2, resultText3;

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

        Button switchButton = (Button) findViewById(R.id.switchButton);

        antText = findViewById(R.id.antText);
                degText = findViewById(R.id.degText);
                spanText = findViewById(R.id.spanText);
                resultText1 = findViewById(R.id.resultLabel1);
                resultText2 = findViewById(R.id.resultLabel2);
                resultText3 = findViewById(R.id.resultLabel3);
            //Listening to button event
            switchButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent nextScreen = new Intent(getApplicationContext(), EngineSecondActivity.class);
                startActivity(nextScreen);

            }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.engine, menu);
            return true;
        }

        public void calculateClickHandler(View view) {
            // get the references to the widgets
            String ant = antText.getText().toString();
            String deg = degText.getText().toString();
            String span = spanText.getText().toString();

            // make sure we handle the click of the calculator button
             if (ant.isEmpty() || deg.isEmpty() || span.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
                } else {
                    float h = Float.parseFloat(ant);
                    float t = Float.parseFloat(deg);
                    float b = Float.parseFloat(span);

                    // calculate disc value
                    float disc1 = calculateDisc1(h, t, b);
                    float disc2 = calculateDisc2(h, t);
                    float disc3 = calculateDisc3(h, t, b);

                    // now set the value in the result text
                    resultText1.setText("d1: " + disc1 + " m");
                    resultText2.setText("d2: " + disc2 + " m");
                    resultText3.setText("d3: " + disc3 + " m");
                }
            }
        }

        // the formula to calculate

        private float calculateDisc1(float h, float t, float b) {

            return (float) (h / Math.abs(Math.tan((t + b / 2) * Math.PI / 180)));
        }

        private float calculateDisc2(float h, float t) {

            return (float) (h / Math.abs(Math.tan(t * Math.PI / 180)));
        }

        private float calculateDisc3(float h, float t, float b) {

            return (float) (h / Math.abs(Math.tan((t - b / 2) * Math.PI / 180)));
        }
    }

答案 4 :(得分:0)

我建议您为所有希望在float中输入值的edittext添加属性 android:inputType =“ numberDecimal”

如果您的edittext不为空,但没有有效的数字,则可能会出现NumberFormatException。为了避免这种情况,请尝试以下

String regEx = "[+-]?([0-9]*[.])?[0-9]+";
Pattern pattern = Pattern.compile(regEx);
boolean validInput = pattern.matcher(ant).matches();
validInput = validInput && pattern.matcher(deg).matches();
validInput = validInput && pattern.matcher(span).matches();
if (!validInput) {
    Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
    return;

} else {
  // parse your inputs here
}

这是修改后的 calculateClickHandler 方法

String regEx = "[+-]?([0-9]*[.])?[0-9]+";
    Pattern pattern = Pattern.compile(regEx);

    public void calculateClickHandler(View view) {
        // get the references to the widgets
        String ant = antText.getText().toString();
        String deg = degText.getText().toString();
        String span = spanText.getText().toString();
        Log.d("calculateClickHandler", "ant =" + ant + " deg=" + deg + " span=" + span);
        boolean validInput = pattern.matcher(ant).matches();
        validInput = validInput && pattern.matcher(deg).matches();
        validInput = validInput && pattern.matcher(span).matches();
        // make sure we handle the click of the calculator button
         if (!validInput) {
                Toast.makeText(getApplicationContext(), "Less Input", Toast.LENGTH_SHORT).show();
         } else {
                float h = Float.parseFloat(ant);
                float t = Float.parseFloat(deg);
                float b = Float.parseFloat(span);

                // calculate disc value
                float disc1 = calculateDisc1(h, t, b);
                float disc2 = calculateDisc2(h, t);
                float disc3 = calculateDisc3(h, t, b);

                // now set the value in the result text
                resultText1.setText("d1: " + disc1 + " m");
                resultText2.setText("d2: " + disc2 + " m");
                resultText3.setText("d3: " + disc3 + " m");
            }
    }

答案 5 :(得分:0)

尝试equalsignorecase(“”) 希望对您有所帮助。

答案 6 :(得分:0)

已由do Project解决>导出前请清理。这是由于APK尚未更新,即使我已经更新了EngineActivity.Java语法,以前在导出为APK时仍使用旧的Activity.Java仍未过滤空字符串(直接在没有IF语句的情况下解析)

当项目移至新PC时,也许是Eclipse错误:(