我收到一条错误说明: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();
}
答案 0 :(得分:0)
您的数据为char[]
,您的方法签名会收到一个字符串,只需添加String.valueOf(data)
。
editor.putString("0", String.valueOf(data));
答案 1 :(得分:0)
您正在尝试将char数组传递给需要字符串的方法。试着这样做:
editor.putString("0", new String(data));