2013-06-27 120 views
0

我收到一個錯誤,指出:方法putString(String,String)類型爲SharedPreferences .Editor不適用於參數(字符串,字符[])SharedPreferences.Editor類型中的putString(String,String)方法不適用於參數(String,char [])

...我不知道爲什麼發生此問題或我如何糾正它。

我想我可以使用:

editor.putLong("0", data); 

但是,這隻會導致:

的方法putLong(字符串,長)的類型SharedPreferences.Editor不適用的參數(字符串,CHAR [])就行發生

錯誤:

editor.putLong( 「0」,數據);

來源:

public class DataCountService extends Service { 
    String text = "USR;1"; 
    String ERROR = Constants.PREFS_NAME; 
    private Timer timer = new Timer(); 
    private long period; 
    private long delay_interval; 

    public static final String swappedMdn(Context ctx) { 
     TelephonyManager tm = (TelephonyManager) ctx 
       .getSystemService(Context.TELEPHONY_SERVICE); 

     // Extract the phone number from the TelephonyManager instance 
     String mdn = tm.getLine1Number(); 

     // Insure MDN is 10 characters 
     if (mdn.length() < 10 || mdn == null) 
      mdn = "0000000000"; 

     // Extract last 10 digits of MDN 
     if (mdn.length() > 10) 
      mdn = mdn.substring(mdn.length() - 10, mdn.length()); 
     char data[] = mdn.toCharArray(); 
     char digit; 
     for (int index = 0; index < mdn.length() - (mdn.length()) % 2; index += 2) { 
      digit = data[index]; 
      data[index] = data[index + 1]; 
      data[index + 1] = digit; 

      Intent i = new Intent(ctx, DataCountService.class); 


       SharedPreferences settings = ctx.getSharedPreferences(Constants.PREFS_NAME, 0); 
       Editor editor = settings.edit(); 
       editor.putString("0", data); 
       editor.commit(); 

     } 

回答

0

您的數據是char[],你的方法簽名接受一個String,只是把String.valueOf(data)

editor.putString("0", String.valueOf(data)); 
+0

@ user2528574 i更新了答案 – nachokk

0

您試圖將字符數組傳遞給需要字符串的方法。嘗試這樣做:

editor.putString("0", new String(data)); 
相關問題