Problems using string for resources

2

I'm creating a quiz app, the link is link .

I'm trying to use getString(R.string.nome) to migrate the strings of the java class and then I can use 2 languages.

In other places I was able to pull the strings and it works by showing the texts when I run the app on my phone, but the moment I try to use the string in the questions, the app just does not open anymore.

public class Questions {

        public String mQuestions[] = {

// Funciona
                "Pergunta número 1 xxxxxxx",

// NÃO FUNCIONA
                getString(R.string.Q1_function_insulin),

        };

/ = / = / = / =

Update:

I made the following changes and it did not work:

My MainActivity.java:

public class MainActivity extends AppCompatActivity {

    Button answer1, answer2, answer3;

    TextView score, question;

    private Questions mQuestions;

    private String mAnswer;
    private int mScore = 0;
    private int mQuestionsLength;

    Random r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mQuestions = new Questions(this);
        mQuestionsLength = mQuestions.mQuestions.length;

        r = new Random();

My other Questions.java file

import android.content.Context;

public class Questions extends MainActivity {

    Context context;
    public Questions(Context context)
    {
        this.context = context;
    }

    public String mQuestions[] = {

            "Pergunta número 1 xxxxxxx",
            context.getString(R.string.Q1_function_insulin),
            "Outra Pergunta número 2",

    };
    
asked by anonymous 16.07.2017 / 19:19

4 answers

0

Luis, you can only call the getStrig method in a class if there is an example context:

public String myString(Context context)
{
return context.getString(R.string.ok); 
}
    
16.07.2017 / 20:35
0

Try this way:

public class Questions
    {
        Context context;
        public Questions(Context context)
        {
            this.context = context;
        }
        public String mQuestions[] = { 
        // Funciona 
        "Pergunta número 1 xxxxxxx",
        // NÃO FUNCIONA 
        context.getString(R.string.Q1_function_insulin), };

        //No "Oncreate" da classe use:


Questions  mQuestions= new Questions(this);
    
17.07.2017 / 01:33
0

The answers already contained can solve the problem, but see below a logical explanation regarding the problem.

getString() is a method that is contained in the abstract class Context . The reason for not working is because you do not extend this class in the Questions method.

For example, if you create your class public class Questions extends Activity , it will work correctly inside the onCreate method. See the schematic below:

Activity
    |
    +----> extends ContextThemeWrapper
                       |
                       +----> extends ContextWrapper 
                                        |
                                        +----> extends Context 

As you yourself said, " Other places I've been able to pull the strings ... ", precisely because these " .. other places .. " you are extending correctly the class containing the method you are using.

Now the solution, there are several, as you can see the answers in your question.

    
17.07.2017 / 17:50
0

At the time you are using the getString() method, the context is null.

Notice that you are instantiating the Questions class at the time of the declaration.

public class MainActivity extends AppCompatActivity {

    Button answer1, answer2, answer3;

    TextView score, question;

    private Questions mQuestions = new Questions();

    private String mAnswer;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        r = new Random();
        ...
        ...
    }
    ...
    ...
} 

Although the Context indirectly extends Activity, the implementation is delegated to a Context that is passed to the constructor. Since the initialization is done in the attribute declaration,

private Questions mQuestions = new Questions();

It is done before the constructor is executed, hence the Context is null.

To resolve create the instance in the onCreate() method:

public class MainActivity extends AppCompatActivity {

    Button answer1, answer2, answer3;

    TextView score, question;

    private Questions mQuestions;

    private String mAnswer;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mQuestions = new Questions();

        r = new Random();
        ...
        ...
    }
    ...
    ...
} 
    
16.07.2017 / 22:41