Ich versuche Benutzereingaben abzurufen, speichern Sie es in den persistenten Daten. Dann möchte ich die Daten verwenden und einen Kreditbetrag berechnen. Ich weiß, dass es etwas mit meinen doppelten Datentypen zu tun hat, und ich habe gesucht und vergeblich versucht. Was mache ich falsch? HierDouble Data Type verursacht App zum Absturz
ist der Code:
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;
}
}
Hier ist der Stack-Trace:
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)
Wissen Sie, wo es abstürzt und mit welchem Fehler – harold
Wenn ich auf "Senden" klicke, stürzt es ab. Ich habe die Doppelgänger debugged und auskommentiert und es funktioniert. aber dann, wenn sie zurückgelegt werden, stürzt es ab. –
Überprüfen Sie [die Stack-Ablaufverfolgung] (http://stackoverflow.com/questions/23353173), um die Ursache des Absturzes festzustellen. Wenn Sie Hilfe beim Lesen und Dolmetschen brauchen, [bearbeiten] Sie Ihre Frage, um das zu posten. –