Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method getString(int) is undefined for the type Apps

How do I fix this error. All the three strings on the bottom get the following error "the method getString(int) is undefined for the type Apps". Please help, im such a noob.

package com.actionbarsherlock.sample.fragments;

import android.content.Context;
import android.content.res.Resources;


public final class Apps {
/**
 * Our data, part 1.
 */
public static final String[] TITLES =
{
        "title1",
        "title2",
        "title3"
};

/**
 * Our data, part 2.
 */
public static final String[] DIALOGUE = { 

    getString(R.string.text1),

    getString(R.string.string2),

    getString(R.string.string3)

};
}
like image 656
idroid8 Avatar asked Oct 21 '12 16:10

idroid8


2 Answers

pass a instance of Context context

and then use

context.getResources().getString(R.string.text1)

here context is belongs to your current activity.

like image 66
Mohsin Naeem Avatar answered Nov 16 '22 05:11

Mohsin Naeem


First getString is not a static method, you are calling it in a static context this can't be done.

Second the getString method is part of the Resources class, your class does not extend the Resources class so the method can't be found.

I think parsing an instance of the Resources class to your Apps class using its constructor would be your the best option.

Something like this:

public final class Apps {

    public Apps(Resources r){
     DIALOGUE = new String[]{
        r.getString(R.string.text1),
        r.getString(R.string.string2),
        r.getString(R.string.string3)};
    }


/**
 * Our data, part 1.
 */
public static final String[] TITLES =
{
        "title1",
        "title2",
        "title3"
};

/**
 * Our data, part 2.
 */
public static String[] DIALOGUE;
}
like image 6
Rolf ツ Avatar answered Nov 16 '22 07:11

Rolf ツ