0

我收到一条错误消息:SharedPreferences.Editor 类型中的 putString(String, String) 方法不适用于参数 (String, char[])

...而且我不确定为什么会出现问题或如何纠正它。

我在想我可以使用:

editor.putLong("0", data);

但这只会导致:

SharedPreferences.Editor 类型中的 putLong(String, long) 方法不适用于参数 (String, 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(); 

        }
4

2 回答 2

0

您正在尝试将 char 数组传递给需要字符串的方法。尝试这样做:

editor.putString("0", new String(data));
于 2013-06-27T20:17:25.060 回答
0

你的数据是char[] ,你的方法签名接收一个字符串,放String.valueOf(data)

editor.putString("0", String.valueOf(data));
于 2013-06-27T20:13:42.697 回答