我有2项活动。在活动A中按下按钮会将您发送到活动B,在B中它要求输入并将该输入返回到A,通过SharedPreference保存它。当我尝试使用SharedPreferences保存该值时,我的应用程序崩溃。我不知道什么是错的。这是活动A代码,它应该保存并显示从活动B传递的值。
P.S。 使用Intent将值从B传递回A没有问题,它只是保存了导致问题的那个值。
TextView teamScore1;
SharedPreferences Score01;
public static String strTScore01 = "MySHaredString";
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Score01 = getSharedPreferences(strTScore01, 0);
String dataReturned01 = Score01.getString("sharedString",
"Cant LOAD data");
teamScore1.setText(dataReturned01);
initialize();
intents();
}
private void initialize()....
private void intents() {
if (getIntent().hasExtra("teamScore1")) {
Intent i = getIntent();
stringData1 = i.getStringExtra("teamScore1");
// SCORE VALUES
if (stringData1.length()> 0) {
//calculations and data manipulation
intTScore_1 = Integer.parseInt(stringData1);
total = intTScore_1 + total; // add to current total
stringData1 = Integer.toString(total);
SharedPreferences.Editor editor = Score01.edit();
editor.putString("sharedString", stringData1);
editor.commit();
Score01 = getSharedPreferences(strTScore01, 0);
String dataReturned1 = Score01.getString("sharedString",
"Cant LOAD data");
teamScore1.setText(dataReturned1);
的logcat .....
11-07 19:36:01.712: D/AndroidRuntime(905): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
11-07 19:36:01.712: D/AndroidRuntime(905): CheckJNI is ON
11-07 19:36:01.832: D/AndroidRuntime(905): --- registering native functions ---
11-07 19:36:02.342: D/AndroidRuntime(905): Shutting down VM
11-07 19:36:02.342: D/dalvikvm(905): Debugger has detached; object registry had 1 entries
11-07 19:36:02.372: I/dalvikvm(905): JNI: AttachCurrentThread (from ???.???)
11-07 19:36:02.372: I/AndroidRuntime(905): NOTE: attach of thread 'Binder Thread #3' failed
11-07 19:36:02.732: D/AndroidRuntime(913): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
11-07 19:36:02.732: D/AndroidRuntime(913): CheckJNI is ON
11-07 19:36:02.852: D/AndroidRuntime(913): --- registering native functions ---
11-07 19:36:03.343: I/ActivityManager(59): Force stopping package com.petermihaylov.android.cardcounter uid=10039
11-07 19:36:03.343: I/Process(59): Sending signal. PID: 898 SIG: 9
11-07 19:36:03.363: I/UsageStats(59): Unexpected resume of com.thenewboston.travis while already resumed in com.petermihaylov.android.cardcounter
11-07 19:36:03.363: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.petermihaylov.android.cardcounter/.MainActivity }
11-07 19:36:03.373: I/WindowManager(59): WIN DEATH: Window{45067ea0 com.petermihaylov.android.cardcounter/com.petermihaylov.android.cardcounter.NewGame paused=false}
11-07 19:36:03.447: D/AndroidRuntime(913): Shutting down VM
11-07 19:36:03.454: D/jdwp(913): Got wake-up signal, bailing out of select
11-07 19:36:03.454: D/dalvikvm(913): Debugger has detached; object registry had 1 entries
11-07 19:36:03.505: I/AndroidRuntime(913): NOTE: attach of thread 'Binder Thread #3' failed
11-07 19:36:03.603: I/ActivityManager(59): Start proc com.petermihaylov.android.cardcounter for activity
com.petermihaylov.android.cardcounter/.MainActivity: pid=920 uid=10039 gids={}
11-07 19:36:03.663: W/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 898 uid 10039
11-07 19:36:03.873: W/ActivityThread(920): Application com.petermihaylov.android.cardcounter is waiting for the debugger on port 8100...
11-07 19:36:03.913: I/System.out(920): Sending WAIT chunk
11-07 19:36:03.923: I/dalvikvm(920): Debugger is active
11-07 19:36:03.953: I/System.out(920): Debugger has connected
11-07 19:36:03.953: I/System.out(920): waiting for debugger to settle...
11-07 19:36:04.193: I/System.out(920): waiting for debugger to settle...
11-07 19:36:04.393: I/System.out(920): waiting for debugger to settle...
11-07 19:36:04.646: I/System.out(920): waiting for debugger to settle...
11-07 19:36:04.843: I/System.out(920): waiting for debugger to settle...
11-07 19:36:05.057: I/System.out(920): waiting for debugger to settle...
11-07 19:36:05.253: I/System.out(920): waiting for debugger to settle...
11-07 19:36:05.513: I/System.out(920): waiting for debugger to settle...
11-07 19:36:05.746: I/System.out(920): waiting for debugger to settle...
11-07 19:36:05.950: I/System.out(920): waiting for debugger to settle...
11-07 19:36:06.154: I/System.out(920): waiting for debugger to settle...
11-07 19:36:06.358: I/System.out(920): debugger has settled (1438)
11-07 19:36:07.377: I/ActivityManager(59): Displayed activity com.petermihaylov.android.cardcounter/.MainActivity: 3784 ms
(total 33895 ms)
11-07 19:36:09.143: I/ActivityManager(59): Starting activity: Intent { act=com.petermihaylov.android.NEWGAME cmp=com.petermihaylov.android.cardcounter/.NewGame (has extras) }
11-07 19:36:10.014: I/ActivityManager(59): Displayed activity com.petermihaylov.android.cardcounter/.NewGame: 761 ms (total 761 ms)
11-07 19:36:14.062: I/ActivityManager(59): Starting activity: Intent { act=com.petermihaylov.android.GAME cmp=com.petermihaylov.android.cardcounter/.Game (has extras) }
11-07 19:36:24.117: W/ActivityManager(59): Launch timeout has expired, giving up wake lock!
11-07 19:36:24.186: W/ActivityManager(59): Activity idle timeout for HistoryRecord{45062c78 com.petermihaylov.android.cardcounter/.Game}
11-07 19:36:29.451: W/ActivityManager(59): Activity destroy timeout for HistoryRecord{4505ae90 com.petermihaylov.android.cardcounter/.NewGame}
经过几个小时的尝试和捕捉声明缩小问题,我想我已经解决了它。这是我希望它可以帮助某人的代码
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
initialize();
intents();
Score01 = getSharedPreferences(strTScore1, 0);
String dataReturned1 = Score01.getString("sharedString",
"Cant LOAD data");
teamScore1.setText(dataReturned1);
}
private void initialize() {...
private void intents() {
if (getIntent().hasExtra("teamScore1")) {
Intent i = getIntent();
stringData1 = i.getStringExtra("teamScore1");
Score01 = getSharedPreferences(strTScore1, 0);
if (stringData1.length() > 0) {
intTScore_1 = Integer.parseInt(stringData1);
total = intTScore_1 + total; // add to current total
stringData1 = Integer.toString(total);
SharedPreferences.Editor editor = Score01.edit();
editor.putString("sharedString", stringData1);
editor.commit();
}
答案 0 :(得分:0)
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
initialize();
intents();
Score01 = getSharedPreferences(strTScore1, 0);
String dataReturned1 = Score01.getString("sharedString",
"Cant LOAD data");
teamScore1.setText(dataReturned1);
}
private void initialize() {...
private void intents() {
if (getIntent().hasExtra("teamScore1")) {
Intent i = getIntent();
stringData1 = i.getStringExtra("teamScore1");
Score01 = getSharedPreferences(strTScore1, 0);
if (stringData1.length() > 0) {
intTScore_1 = Integer.parseInt(stringData1);
total = intTScore_1 + total; // add to current total
stringData1 = Integer.toString(total);
SharedPreferences.Editor editor = Score01.edit();
editor.putString("sharedString", stringData1);
editor.commit();
}