2011-11-08 7 views
0

私は2つのアクティビティを持っています。アクティビティAでは、ボタンを押すとBアクティビティが送信され、Bでは入力を求められ、その入力がSharedPreferences経由で保存されます。SharedPreferencesを使用してその値を保存しようとすると、アプリがクラッシュします。私は何が間違っているか把握できません。アクティビティAのコードは、アクティビティBから渡された値を保存して表示する必要があります。インテント文字列を渡すと共有プレースデータが保存されない...アクティビティがクラッシュする

p.s. インテントを使用して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 .....私は私はそれを解決したと思う問題を絞り込んtryおよびcatch文のいくつかの長い時間後

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} 

。ここで私はそれはあなたがより良いここでlogcatを貼り付けていたので、私たちはあなたの問題を特定することができる人

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

を役に立てば幸いコードがあります。 – Huang

+0

私は最初にAを始めるとき、インテントにはエクストラが含まれていないので、stringData1はnullだと思います。これがあなたの問題です。 – Huang

+0

onCreateメソッドのsharedpreferencesコードを削除しても同じエラーが発生しています...もしStringData1のデータがあってもアプリケーションがクラッシュすると、エラーログを貼り付けるときには –

答えて

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(); 

      } 
関連する問題