我是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 :(得分:3)
final EditText numberone = (EditText)findViewById(R.id.number1);
final EditText numbertwo = (EditText)findViewById(R.id.number2);
在setContentView()
另外, 当sharedPref中没有任何内容时,第一次会发生什么,
String number1 = mySharedPreferences.getString("number1", null);
String number2 = mySharedPreferences.getString("number2", null);
返回一些值,而不是“null”。
如果经常为Calculator应用程序使用SharedPref,我建议为get获取并放置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);
}
如果函数在您的活动中,则可以全局声明该活动的上下文。否则在论证中传递它。
答案 1 :(得分: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);
}
答案 2 :(得分: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);
{{1}}
答案 3 :(得分:0)
您最有可能获得NULL指针异常。这是因为,当您的应用程序共享优先级没有任何内容时,您倾向于访问它并从中获取值,当它不存在时,您设置字符串NULL的值。当您尝试在屏幕上显示该值为NULL时,它会崩溃。 这样做:
String number1 = mySharedPreferences.getString("number1", "");
String number2 = mySharedPreferences.getString("number2", "");
答案 4 :(得分: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),并由您的应用程序添加您想要保存在州外的任何状态数据。
当用户恢复您的Activity时,如果您的Activity在上述场景中被终止,则会调用onCreate(Bundle savedInstanceState)并且savedInstanceState将为非null。然后,您的应用可以从savedInstanceState中获取数据,并将Activity的状态重新生成用户上次看到它时的状态。
基本上在onCreate中,当savedInstanceState为null时,则表示这是您的Activity的“全新”启动。当它为非null时(如果你的应用程序将数据保存在onSaveInstanceState(...)中,则表示需要重新创建Activity状态。
Here更详细的信息