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

4

8

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)

};
}
Evelinaeveline answered 21/10, 2012 at 16:35 Comment(4)
You're trying to call a non-existent method. There's really not much we can do to help you fix it without knowing what you're trying to do.Levee
Are you meaning to extend something? What are you trying to subclass?Parrisch
the person helping me with this said "getString is from the activity class, so your class needs to inherit from activity, or you need to call getString from a class that already inherits from activity."Evelinaeveline
What im trying to do is call those three strings they hold text valueEvelinaeveline
G
6

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;
}
Garment answered 21/10, 2012 at 16:38 Comment(2)
Got rid of the error but I have to fix something else I'll brb with the results :)Evelinaeveline
mhhh when i select a category then app title the app force closes. Ill play around more but your answer was the most helpful.Evelinaeveline
E
32

pass a instance of Context context

and then use

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

here context is belongs to your current activity.

Exterritorial answered 21/10, 2012 at 16:40 Comment(1)
I followed this and rewrote Preferences.getBoolean(getString(R.string.pref_mypreference), false) to Preferences.getBoolean(***getContext().***getString(R.string.pref_mypreference), false) and Bob became my uncle :D (the *** are not literal, but showing what changed only) ;)Otilia
G
6

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;
}
Garment answered 21/10, 2012 at 16:38 Comment(2)
Got rid of the error but I have to fix something else I'll brb with the results :)Evelinaeveline
mhhh when i select a category then app title the app force closes. Ill play around more but your answer was the most helpful.Evelinaeveline
C
2

Create a subclass of Application, for instance public class App extends Application { Set the android:name attribute of your tag in the AndroidManifest.xml to point to your new class, e.g. android:name=".App" In the onCreate() method of your app instance, save your context (e.g. this) to a static field named mContext and create a static method that returns this field, e.g. getContext():

This is how it should look:

public class App extends Application{

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static Context getContext(){
        return mContext;
    }
}

Now you can use: App.getContext().getString()

Clarify answered 5/7, 2018 at 8:46 Comment(0)
H
1

you could try

getResources().getString(R.string.somestrid);

or

getApplicationContext().getResources().getString(R.string.somestrid);
Helicline answered 10/11, 2019 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.