4

A quick question:

I am getting the error : cannot convert from String to Editable. Here is the code :

Credential.getInstance().UserName = preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = preferences.getString("UserName", "invalid value");

I've also tried casting like following:

Credential.getInstance().UserName = (Editable) preferences.getString("UserName", "invalid value");
Credential.getInstance().Password = (Editable) preferences.getString("UserName", "invalid value");

This time I am getting error : Cannot cast from String to Editable.

What to do?

Sam
  • 7,252
  • 16
  • 46
  • 65
necixy
  • 4,964
  • 5
  • 38
  • 54

2 Answers2

6

For anyone interested in converting a String to Editable

Editable.Factory.getInstance().newEditable("myString");

From the docs, append is

Convenience for append(String.valueOf(text)).

Maragues
  • 37,861
  • 14
  • 95
  • 96
5

Surely:

Credential.getInstance().UserName.append(preferences.getString("UserName", "invalid value"));

The API with the answers:

http://developer.android.com/reference/android/text/Editable.html

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • I am getting this error: The method setText(String) is undefined for the type Editable. I am using eclipse and android 1.6. – necixy Apr 18 '11 at 12:43
  • append method gives the NullPointerException. – necixy Apr 18 '11 at 12:51
  • Yes this is your next error, but this one is fixed. One of your variables is null, UserName or preferences. Mark answered, look at your LogCat and go fix :-) – Blundell Apr 18 '11 at 12:53
  • I appreciate your co-operation, I have also used this code, `Credential.getInstance().UserName.append("a static value");` but this fix value also give the NullPointerException. What about this one? – necixy Apr 18 '11 at 12:55
  • Credential.getInstance() may be returning null. You should debug this and find out :p – Blundell Apr 18 '11 at 12:58
  • I have also tried this: `Credential cre = Credential.getInstance(); if(cre==null) { Log.w("is error there?","Is error there?"); }` by this I am sure that Credential.getInstance() isn't returning a null value. Should I instantiate the UserName variable(which is the Editable variable)? If yes then how? – necixy Apr 18 '11 at 13:05
  • Yes if UserName is null then this is your problem. I don't know because I don't know how your credential class works :-) – Blundell Apr 18 '11 at 13:09
  • No Problem, finally changed the Credential Class and changed the data type of UserName from Editable to String. So my problem has been sorted out in a different way from this question. I couldn't test the answer myself yet after trusting you I marked the answer corrected just because I don't want someone else to get frustrated for a couple of hour just for tiny thing. :) – necixy Apr 18 '11 at 13:22