2014-02-26 27 views
-1

我是android編程的新手,我創建了一個簡單的計算器。我希望用戶能夠暫停應用程序而不會丟失在兩個EditText框中輸入的數字。我正在嘗試使用共享首選項,但由於某種原因,我的應用程序不斷崩潰。我怎樣才能解決這個問題?使用共享首選項來保存編輯文本中的數字

package com.example.simplecalculator; 

import android.os.Bundle; 


import android.app.Activity; 
import android.content.SharedPreferences; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.example.simplecalculator.*; 


public class MainActivity extends Activity { 

SharedPreferences mySharedPreferences; 
SharedPreferences.Editor editor; 
public static final String MYPREFERENCES = "MyPreferences_001"; 
float answer; 
final EditText numberone = (EditText)findViewById(R.id.number1); 
final EditText numbertwo = (EditText)findViewById(R.id.number2); 

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

    mySharedPreferences = getSharedPreferences(MYPREFERENCES, 0); 
    String number1 = mySharedPreferences.getString("number1", null); 
    String number2 = mySharedPreferences.getString("number2", null); 
    numberone.setText(number1); 
    numbertwo.setText(number2); 



} 

@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; 
} 

public void Addition(View view) { 

answer = Float.parseFloat(numberone.getText().toString()) + Float.parseFloat(numbertwo.getText().toString());; 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your sum is " + answer); 


} 

public void Subtraction(View view) { 

answer = Float.parseFloat(numberone.getText().toString()) - Float.parseFloat(numbertwo.getText().toString());; 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your difference is " + answer); 

} 


public void Multiplication(View view) { 
    answer = Float.parseFloat(numberone.getText().toString()) * Float.parseFloat(numbertwo.getText().toString());; 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your product is " + answer); 

} 

public void Division(View view) { 

    answer = Float.parseFloat(numberone.getText().toString())/Float.parseFloat(numbertwo.getText().toString());; 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your quotient is " + answer); 

} 

public void Power(View view) { 
    answer = (float) Math.pow(Float.parseFloat(numberone.getText().toString()), Float.parseFloat(numbertwo.getText().toString())); 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your answer is " + answer); 

} 

public void Root(View view) { 

    answer = (float) Math.sqrt(Float.parseFloat(numberone.getText().toString())); 

TextView value = (TextView)findViewById(R.id.answer); 
value.setText("Your answer is " + answer); 

} 


public void onPause(){ 

    mySharedPreferences = getSharedPreferences(MYPREFERENCES, Activity.MODE_PRIVATE); 
    editor = mySharedPreferences.edit(); 
    editor.putString("number1", numberone.getText().toString()); 
    editor.putString("number2", numbertwo.getText().toString()); 
    editor.commit(); 

} 

} 
+0

您可能正在獲取NumberFormatException。 – Kedarnath

+0

那是什麼,我該如何解決?再次抱歉,我是新手。 – user3246935

回答

2

您的應用程序崩潰是因爲您在將任何佈局設置爲您的活動之前嘗試初始化您的視圖。現在,初始化如下...你的問題將被解決。

EditText numberone; 
EditText numbertwo; 

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

    numberone = (EditText)findViewById(R.id.number1); 
    numbertwo = (EditText)findViewById(R.id.number2); 

} 
+0

這工作!謝謝!! – user3246935

+0

那你能接受答案嗎? :) –

0

試試這個..

你必須用來初始化您EditText之前onCreate做內部onCreate

float answer; 
final EditText numberone; 
final EditText numbertwo; 

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

     numberone = (EditText)findViewById(R.id.number1); 
     numbertwo = (EditText)findViewById(R.id.number2); 
3
final EditText numberone = (EditText)findViewById(R.id.number1); 
final EditText numbertwo = (EditText)findViewById(R.id.number2); 

保持裏面的onCreate(),之後setContentView()

而且, 什麼hap第一次,當沒有任何東西存儲在sharedPref中時,

String number1 = mySharedPreferences.getString("number1", null); 
    String number2 = mySharedPreferences.getString("number2", null); 

返回一些值,而不是「null」。

如果使用SharedPref很經常的計算器應用程序,我建議作出了獲得的功能,並把sharedPref ..
喜歡的東西:

public static void saveDataToPreferences(String key, 
      String value) { 
     SharedPreferences prefs = context.getSharedPreferences("your package name", 
       Context.MODE_PRIVATE); 
     Editor editor = prefs.edit(); 
     editor.putString(key, value); 
     editor.commit(); 
    } 
    public static String getDataFromPreferences(String key) { 

     SharedPreferences prefs = context.getSharedPreferences("your package name", 
       Context.MODE_PRIVATE); 
     return prefs.getString(key, Constants.BLANK); 
    } 

如果函數是你的活動中,爲上下文該活動可以在全球範圍內宣佈。否則在論據中通過。

0

您正在獲得NULL指針異常最有可能。它的原因是,首先當你的應用程序共享提示沒有任何東西時,你傾向於訪問它並從它獲得一個值,當它不存在時,你設置字符串NULL的值。當您嘗試在屏幕上顯示NULL值時,它會崩潰。 這樣做:

String number1 = mySharedPreferences.getString("number1", ""); 
String number2 = mySharedPreferences.getString("number2", ""); 
0

我認爲你需要使用onSaveInstanceState()onRestoreInstanceState()用於這一目的。

當活動被activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish()the OS (ex: when your Activity is in the background and another task needs resources)強行終止時使用。發生這種情況時,onSaveInstanceState(Bundle outstate)將被調用,並且取決於您的應用程序添加要保存在outstate中的任何狀態數據。

當用戶恢復您的Activity時,如果您的Activity在上述場景中被終止,則onCreate(Bundle savedInstanceState)被調用並且savedInstanceState將爲非null。然後,您的應用程序可以從savedInstanceState中獲取數據,並將您的活動狀態重新生成爲用戶上次看到它時的狀態。

基本上在onCreate,當savedInstanceState爲null時,這意味着這是一個'新鮮'啓動你的活動。當它非空(如果您的應用程序將數據保存在onSaveInstanceState(...)中,則意味着需要重新創建Activity狀態。

Here有關它的更詳細信息