2015-08-15 102 views
0

我有36個按鈕(我在這個問題中只顯示2)在活動Pim。每個按鈕在我的共享首選項中分配/保存一個密鑰,這些密鑰在活動S1m中加載,但問題是它從不加載我的密鑰。共享偏好不會加載在Android

P1M

public class P1m extends Activity { 

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

    public void ten(View view) { 
     Toast toast = Toast.makeText(this, "Plan saved!", Toast.LENGTH_SHORT); 
     toast.show(); 
     SharedPreferences preferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("train", "p1m_10"); 
     editor.commit(); 
     finish(); 
     Intent intent = new Intent(this, MainMenu.class); 
     startActivity(intent); 
    } 

    public void fifteen(View view) { 
     Toast toast = Toast.makeText(this, "Plan saved!", Toast.LENGTH_SHORT); 
     toast.show(); 
     SharedPreferences preferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("train", "p1m_15"); 
     editor.commit(); 
     finish(); 
     Intent intent = new Intent(this, MainMenu.class); 
     startActivity(intent); 
    } 
} 

S1M

public class S1m extends Activity implements IBaseGpsListener { 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show(); 
     timer.cancel(); 
    } 

    private TextView q; 
    private CountDownTimer timer; 
    private Vibrator mVibrator; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sprint_ui); 
     LocationManager locationManager = (LocationManager) this 
       .getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
       0, this); 
     this.updateSpeed(null); 

     CheckBox chkUseMetricUntis = (CheckBox) this 
       .findViewById(R.id.chkMetricUnits); 
     chkUseMetricUntis 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // TODO Auto-generated method stub 
         S1m.this.updateSpeed(null); 
        } 
       }); 

     // LOAD SHARED PREF P1m//////////////////////////////////////////// 

     SharedPreferences preferences11 = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     String name2 = preferences11.getString("train", ""); 

     if (name2.equals("p1m_10")) { 
      final Handler handler = new Handler(); 
      Timer timer = new Timer(); 
      timer.schedule(new TimerTask() { 
       private Vibrator mVibrator; 

       public void run() { 
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
        mVibrator.vibrate(1000 * 10);// sprint time vibration 
        // HANDLER 
        Runnable runnable = new Runnable() { 
         public void run() { 
          // calculate result1 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result1); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result1 
         } 
        }; 
        handler.postDelayed(runnable, 3333); 
        // HANDLER END // 
        // HANDLER 
        Runnable runnable1 = new Runnable() { 
         public void run() { 
          // calculate result2 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result2); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result2 
         } 
        }; 
        handler.postDelayed(runnable1, 6666); 
        // HANDLER END // 
        // HANDLER 
        Runnable runnable11 = new Runnable() { 
         public void run() { 
          // calculate result3 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result3); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result3 
         } 
        }; 
        handler.postDelayed(runnable11, 9999); 
        // HANDLER END // 
        // warning 
        Runnable runnable111 = new Runnable() { 
         public void run() { 
          Toast.makeText(
            getBaseContext(), 
            "*you have 5 seconds left. get ready for the next sprint!*", 
            Toast.LENGTH_SHORT).show(); 
          mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
          mVibrator.vibrate(1000); 
         } 
        }; 
        handler.postDelayed(runnable111, 10000 + 60000 - 5000);// warning 
                      // after 
        // warning // 
       } 
      }, 5999, 10000 + 60000);// repeat after (+5.999s first run delay) 

     } else if (name2.equals("p1m_15")) { 
      final Handler handler = new Handler(); 
      Timer timer = new Timer(); 
      timer.schedule(new TimerTask() { 
       private Vibrator mVibrator; 

       public void run() { 
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
        mVibrator.vibrate(1000 * 15);// sprint time vibration 
        // HANDLER 
        Runnable runnable = new Runnable() { 
         public void run() { 
          // calculate result1 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result1); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result1 
         } 
        }; 
        handler.postDelayed(runnable, 5000); 
        // HANDLER END // 
        // HANDLER 
        Runnable runnable1 = new Runnable() { 
         public void run() { 
          // calculate result2 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result2); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result2 
         } 
        }; 
        handler.postDelayed(runnable1, 10000); 
        // HANDLER END // 
        // HANDLER 
        Runnable runnable11 = new Runnable() { 
         public void run() { 
          // calculate result3 
          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed); 
          String shareFact = theFact.getText().toString(); 
          TextView theFact1 = (TextView) findViewById(R.id.result3); 
          theFact1.setText(String.valueOf(shareFact)); 
          // calculate result3 
         } 
        }; 
        handler.postDelayed(runnable11, 15000); 
        // HANDLER END // 
        // warning 
        Runnable runnable111 = new Runnable() { 
         public void run() { 
          Toast.makeText(
            getBaseContext(), 
            "*you have 5 seconds left. get ready for the next sprint!*", 
            Toast.LENGTH_SHORT).show(); 
          mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
          mVibrator.vibrate(1000); 
         } 
        }; 
        handler.postDelayed(runnable111, 15000 + 60000 - 5000);// warning 
                      // after 
        // warning // 
       } 
      }, 5999, 15000 + 60000);// repeat after (+5.999s first run delay) 

      // LOAD SHARED PREF P1m//////////////////////////////////////////// 
     } 
    } 

    public void finish() { 
     super.finish(); 
     System.exit(0); 
    } 

    private void updateSpeed(CLocation location) { 
     // TODO Auto-generated method stub 
     float nCurrentSpeed = 0; 

     if (location != null) { 
      location.setUseMetricunits(this.useMetricUnits()); 
      nCurrentSpeed = location.getSpeed(); 
     } 

     Formatter fmt = new Formatter(new StringBuilder()); 
     fmt.format(Locale.US, "%5.1f", nCurrentSpeed); 
     String strCurrentSpeed = fmt.toString(); 
     strCurrentSpeed = strCurrentSpeed.replace(' ', '0'); 

     String strUnits = "miles/hour"; 
     if (this.useMetricUnits()) { 
      strUnits = "meters/second"; 
     } 

     TextView txtCurrentSpeed = (TextView) this 
       .findViewById(R.id.txtCurrentSpeed); 
     txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits); 
    } 

    private boolean useMetricUnits() { 
     // TODO Auto-generated method stub 
     CheckBox chkUseMetricUnits = (CheckBox) this 
       .findViewById(R.id.chkMetricUnits); 
     return chkUseMetricUnits.isChecked(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 
     if (location != null) { 
      CLocation myLocation = new CLocation(location, 
        this.useMetricUnits()); 
      this.updateSpeed(myLocation); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onGpsStatusChanged(int event) { 
     // TODO Auto-generated method stub 

    } 

} 

我的按鈕做工精細,因爲總是顯示爲它們分配吐司。

+0

在P1m中,每次創建按鈕時都會創建SharedPreferences即時變量。在onCreate()期間只創建SharedPreferences和Editor變量一次,並在onClick()方法中使用它們來分配鍵值對。 – adhithiyan

+0

@adhithiyan它沒有工作 – MaggotSauceYumYum

+0

你會驚訝我在我的其他活動(P15,S15)使用相同的代碼,它工作正常,我只是複製粘貼代碼在他們所有。其工作正常p15,p20,p30,但不適用於p1m,p2m,p3m,p5m和p7m – MaggotSauceYumYum

回答

0

爲了更好的代碼,創建常量文件並根據需要在您的Java類中使用常量文件,如下所示。

public class Constants { 

    public static SharedPreferences prefs; 
    public static SharedPreferences.Editor editor; 

} 

你P1M類

public class P1m extends Activity { 

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

public void ten(View view) { 
    Toast toast = Toast.makeText(this, "Plan saved!", Toast.LENGTH_SHORT); 
    toast.show(); 

    Constants.prefs = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE); 
    Constants.editor = Constants.prefs.edit(); 

    Constants.editor.putString("train", "p1m_10"); 
    Constants.editor.commit(); 

    finish(); 
    Intent intent = new Intent(this, MainMenu.class); 
    startActivity(intent); 
} 

public void fifteen(View view) { 
    Toast toast = Toast.makeText(this, "Plan saved!", Toast.LENGTH_SHORT); 
    toast.show(); 

    Constants.editor.putString("train", "p1m_15"); 
    Constants.editor.commit(); 
    finish(); 
    Intent intent = new Intent(this, MainMenu.class); 
    startActivity(intent); 
    } 
} 

在您的S1M類,得到這樣follwoing路徑值,

Constants.prefs = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE); 
Constants.editor = Constants.prefs.edit(); 
String train = Constants.prefs.getString("train", "your default train"); 

此之後,你順利地拿到你的價值和你的代碼得到了改善。

+0

你好@毒性有沒有解決方案? –

+0

還沒有,請閱讀我的評論下面的問題 – MaggotSauceYumYum

+0

嘗試作爲我的回答? –