我有一个textView值,可以使用加号和减号按钮更改。 (比如机器人编号选择器对象,但是自定义。)
我需要能够提取每个值并保存,具体取决于用户设置的微调器值。
对于为每个微调器位置保存的每个值,当用户返回微调器上的相同位置时,将显示输入的先前值,表示已输入值。
For Instance :(如果微调器是星期几)(值是TextView /自定义微调器)“如果用户选择星期三,并选择值7,则转到星期一并选择8,如果用户返回到星期三之前输入的值(7)将显示,如果用户返回星期一,则将显示值8。
final Spinner spinner1 = (Spinner) findViewById(R.id.sp_dotw);
final ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.dotw_array, R.layout.custom_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner1position = position;
spinnerPos();
showToast("spinnerdotw: position= " + position + "id=" + id);
}
public void onNothingSelected(AdapterView<?> adapterView) {
showToast("No Dotw selected");
}
});
@Override
public void onClick(View v) {
String getString = String.valueOf(tvSony.getText());
int current1 = Integer.parseInt(getString);
if (v == btnUp1) {
if (current1 < nEnd) {
current1++;
tvSony.setText(String.valueOf(current1));
}
}
if (v == btnDown1) {
if (current1 > nStart) {
current1--;
tvSony.setText(String.valueOf(current1));
}
}
sonyRs = current1;
}
SharedPreferences sp = getSharedPreferences("sun32save", MODE_PRIVATE);
SharedPreferences.Editor spedit = sp.edit();
if (spinner1position == 0 & spinner2position == 0) {
spedit.putString("sun32Son", String.valueOf(sonyRs));
String savedValue1 = sp.getString("sun32Son", "");
spedit.commit();
tvSony.setText(savedValue1);
}
//if a value is found, display value
if (spinner1position == 0 & spinner2position == 1) {
spedit.putString("sun35Son", String.valueOf(sonyRs));
String savedValue2 = sp.getString("sun35Son", "");
spedit.commit();
tvSony.setText(savedValue2);
如果我返回到相同的微调器位置,则返回默认值0。
06-01 18:37:51.503 18264-18264/com.example.liam.quickreport20 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.liam.quickreport20, PID: 18264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liam.quickreport20/com.example.liam.quickreport20.UnitsForm}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.example.liam.quickreport20.UnitsForm$Cache.get(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.example.liam.quickreport20.UnitsForm$Cache.get(java.lang.Object)' on a null object reference
at com.example.liam.quickreport20.UnitsForm.loadTextFields(UnitsForm.java:220)
at com.example.liam.quickreport20.UnitsForm.onCreate(UnitsForm.java:152)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)