R.string.value Help android notification

后端 未结 4 1142
粉色の甜心
粉色の甜心 2021-02-12 10:48

whats the deal with

 CharSequence contentTitle = R.string.value;

Error cannot convert from int to CharSequence. Is there a way around this or a

4条回答
  •  礼貌的吻别
    2021-02-12 11:19

    R.string.value returns the reference ID number of the resource 'value'. If you look at your R class it will appear as something like this:

    public static final class string {
      public static final int value=0x7f040007;
    }
    

    I've been experiencing issues with referencing the getString() method. The exact error that Eclipse spits at me is:

    The method getString(int) is undefined for the type DatabaseHelper.MainDatabaseHelper

    After reading for awhile I've figured out that you must reference your application's context to get access to the getString() method. I was trying to create a private SQLDatabase helper class in a content provider, however, that was not allowing me to reference the getString() method. My solution so far is to do something like this:

    private class MainDatabaseHelper extends SQLiteOpenHelper {
    
        MainDatabaseHelper(Context context) {
            super(context, context.getString(R.string.createRoutesTable), null, 1);
        }
    
        public void onCreate(SQLiteDatabase db) {
            db.execSQL((getContext()).getString(R.string.createRoutesTable));
        }
    }
    

    Notice these two context references:

    context.getString()

    (getContext()).getString()

    I don't know if this is the optimal long-term solution but it seems to work for the moment. Hope this helps.

提交回复
热议问题