2017-04-22 79 views
0

我试图检索用户输入,并将其存储在持久数据中。然后我想使用这些数据并计算贷款金额。我知道这与我的双重数据类型有关,我已经搜索并试图无济于事。我究竟做错了什么?双重数据类型导致应用程序崩溃

下面是代码:

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) 
+0

你知道它在哪里崩溃,并与什么错误 – harold

+0

当我点击提交崩溃。我已调试并注释掉双打,它的工作原理。但是当它们被放回时它会崩溃。 –

+0

检查[堆栈跟踪](http://stackoverflow.com/questions/23353173)以确定崩溃的原因。如果您在阅读和解读时需要帮助,请编辑您的问题以发布。 –

回答

0

应用崩溃,因为输入的值不能被解析成两倍。

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

堆栈跟踪显示错误,您输入了单词“repayments”,显然它不能被解析为double。

您应该按如下所示使用类型为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(); 
      } 
     } 
+0

它真的很棒。它已经停止了崩溃。但是现在if语句不起作用。我如何将if语句与异常的结果链接起来? –

+0

如果if语句不起作用,你是什么意思?在上面的代码中,如果文本为空,则异常不会到达。解析后尝试里面的过程值。 –

+0

嗨。谢谢。我昨天试过了,幸好它工作正常。谢谢您的帮助。非常感激。 –