双重数据类型导致应用崩溃

时间:2017-04-22 19:39:13

标签: java android types

我正在尝试检索用户输入,将其存储在持久数据中。然后我想使用数据并计算贷款金额。我知道它与我的双数据类型有关,我搜索过并试图无济于事。我做错了什么?

以下是代码:

package com.example.s35505109;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
String strID;
String strSurname;
String strAddress; 
String strGuardName;
String strGuardID; 
String strGender; 
String strOutcome;
String douLoanAmt;
String doumonInc;

double dou_monInc = 0;

double douRepayments = 0;

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


 // get inputed data from user


 //code the submit button 

  Button but1 = (Button)findViewById(R.id.buttSubmit);
  final SharedPreferences sharedPref = 
PreferenceManager.getDefaultSharedPreferences(this);
  but1.setOnClickListener(new OnClickListener(){

     public void onClick(View v){

     //This is where your code will go
    // retrieve data from input
         EditText ID = (EditText)findViewById(R.id.editTextID);
         EditText surname = (EditText)findViewById(R.id.editTextSurname);
         Spinner gender = (Spinner)findViewById(R.id.txtGender);
         EditText loanAmt = (EditText)findViewById(R.id.editTextLoanAmt);
         EditText address = (EditText)findViewById(R.id.editTextAddress);
         EditText guardname = (EditText)findViewById(R.id.editTextGuarName);
         EditText guardID = (EditText)findViewById(R.id.editTextGuarID);
         EditText monInc = (EditText)findViewById(R.id.editTextMoInc);          
         TextView repayments = (TextView)findViewById(R.id.textViewRepayments);
         TextView outcome = (TextView)findViewById(R.id.textView1);

         strID = ID.getText().toString();
         strSurname = surname.getText().toString();
         strGender = gender.getSelectedItem().toString();
         douLoanAmt = loanAmt.getText().toString();
         if (!TextUtils.isEmpty(douLoanAmt)) {
             // call parseDouble in here
             double dou_LoanAmt = Double.parseDouble(douLoanAmt);
         }           
         strAddress = address.getText().toString();
         strGuardName = guardname.getText().toString();
         strGuardID = guardID.getText().toString();
         doumonInc = monInc.getText().toString();
         if (!TextUtils.isEmpty(doumonInc)) {
             // call parseDouble in here
             double dou_monInc = Double.parseDouble(doumonInc);
         }           


        if (dou_monInc < 5000 ){
             outcome.setText("You do not qualify for a loan");
         ;}
        else if (dou_monInc>5000 && dou_monInc<10000 ){
             outcome.setText("You qualify for a maximum loan of R40,000 at 15% interest");}
        else if (dou_monInc>10000 && dou_monInc<30000 ){
             outcome.setText("You qualify for a maximum loan of R70,000 at 16.5% interest");}
        else if (dou_monInc>30000 ){
             outcome.setText("You qualify for a maximum loan of R70,000 at 16.5% interest");}
        else {outcome.setText("");}     


         douRepayments = Float.parseFloat(repayments.getText().toString());
         SharedPreferences.Editor editor = sharedPref.edit();
         editor.putString("key1", strID);
         editor.putString("key2", strSurname);
         editor.putString("key3", strGender);
         editor.putString("key4", strAddress);
         editor.putString("key5", strGuardName);
         editor.putString("key6", strGuardID);
         editor.putString("key7", strOutcome);
         //editor.putFloat("key8", douLoanAmt);
         //editor.putFloat("key9", doumonInc);
         //editor.putFloat("key10", douRepayments);
         editor.commit();
         }
   });  //end but1

}



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

}

这是堆栈跟踪:

04-23 08:17:10.178: E/AndroidRuntime(2690): in writeCrashedAppName, pkgName 
:com.example.s35505109
04-23 08:17:10.228: E/AndroidRuntime(2690): FATAL EXCEPTION: main
04-23 08:17:10.228: E/AndroidRuntime(2690): Process: com.example.s35505109, 
PID: 2690
04-23 08:17:10.228: E/AndroidRuntime(2690): java.lang.NumberFormatException: 
Invalid float: "Repayments:"
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.invalidReal(StringToReal.java:63)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.initialParse(StringToReal.java:114)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.StringToReal.parseFloat(StringToReal.java:304)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.Float.parseFloat(Float.java:300)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.example.s35505109.MainActivity$1.onClick(MainActivity.java:91)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.view.View.performClick(View.java:4560)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.view.View$PerformClick.run(View.java:18636)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Handler.handleCallback(Handler.java:733)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Handler.dispatchMessage(Handler.java:95)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.os.Looper.loop(Looper.java:136)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
android.app.ActivityThread.main(ActivityThread.java:5021)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.reflect.Method.invokeNative(Native Method)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
java.lang.reflect.Method.invoke(Method.java:515)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:827)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-23 08:17:10.228: E/AndroidRuntime(2690):     at 
dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

由于输入的值导致应用程序崩溃无法解析为double。

04-23 08:17:10.228: E/AndroidRuntime(2690): java.lang.NumberFormatException: 
Invalid float: "Repayments:"

堆栈跟踪显示错误,您已输入word&#34;还款&#34;显然它不能被解析为双倍。

您应该使用类型为numberDecimal|numberSigned的EditText,如下所示,然后向用户显示数字小键盘,然后它就不会出错了。

<EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal|numberSigned"
        android:text="Name"
        tools:layout_editor_absoluteX="84dp"
        tools:layout_editor_absoluteY="103dp" />

然后你需要处理NumberFormatException为Follows。

if (!TextUtils.isEmpty(douLoanAmt)) {
            try {
                // call parseDouble in here
                double dou_LoanAmt = Double.parseDouble(douLoanAmt);
                // it means it is double, process
            } catch (Exception e1) {
                // this means it is not double 
                // show error message
                e1.printStackTrace();
            }
        }