3
I’m creating an app quiz, the link is https://github.com/luisbalmant/QuickQuiz-Science.
I’m trying to use the getString(R.string.nome)
to migrate the java class strings and then be able to use 2 languages.
In other places I managed to pull the strings and is working showing the texts when I run the app on mobile, but the moment I try to use the string in questions the app simply does not open more.
public class Questions {
public String mQuestions[] = {
// Funciona
"Pergunta número 1 xxxxxxx",
// NÃO FUNCIONA
getString(R.string.Q1_function_insulin),
};
=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=//=/=/=/=/=/=/=/=
Updating:
I made the following changes and it didn’t 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",
};
Even doing so the problem will continue to exist if the class Questions is instantiated at the time it is declared (as attribute), in this case so
private Questions mQuestions = new Questions(this);
. The problem is that at that point the context is null. The only solution is to instantiate it in theonCreate()
or in another place after it.– ramaral
@ramaral so I said "within the onCreate method". = D
– viana
Yes but in that case you do not need to pass the contex. The class Questions, as it is a Inner class(not Static) of Activity, you have access to the context/Activity method. All your explanation does not apply in this context.
– ramaral
@ramaral The explanation is focusing on why the getString method didn’t work. Do you think it’s unnecessary to demonstrate this to the user?! I will leave the solution part disabled and then edit in a clearer form.
– viana
The reason, in this case, why getString() does not "work" is because Context is null. It is not because the class does not extend from Context or because one is not passed to the constructor.
– ramaral
@ramaral I think the two things right?!
– viana
It’s not. Look at the Jefferson answer, a context is passed and it still won’t work.
– ramaral
Hello ramaral and @acklay. First thank you for all the explanations. I’m starting learning now and from what I understand here and reading in some materials I saw that a possible solution is to instantiate it in onCreate(), but I don’t know how to do that. I tried several things, but as I am learning I read and I go in the code and "kick" and keep going wrong. Is there any simpler alternative than me suddenly relocating or redoing my public String mQuestions[] = {... ?
– Luís Balmant
@Luísbalmant I tested the Questions class and it works by making the changes I mentioned in my reply, which is no more than instantiating it in
onCreate()
. If it doesn’t work, it’s because you now have another error (I haven’t tested your entire code). Add a comment with the error.– ramaral
@ramaral, thanks for your attention and patience. I’m not sure if I could find the error but I think it’s as follows: Error running Questions: The Activity 'Questions' is not declared in Androidmanifest.xml
– Luís Balmant
I added in Androidmanifest.xml the following: <Activity android:name=". Questions" /> and a message appears that does not have a default constructor
– Luís Balmant
@Luísbalmant I think I ended up confusing you and totally messing up your head. It’s hard to even explain here. Ramaral’s response is the most appropriate for your case. I gave just one example of how it could work extending the Activity class in its Questions class; Try to do it calmly, read a little more and understand the life cycle of how an Activity works; know the concept of Abstraction, Encapsulation and Inheritance?! Try to read a little more to understand better. I think it will buy more time instead of trying to go kicking. Soon you will laugh at your own questions. he
– viana
@acklay. Thank you, I will do what you recommended. The worst was that I took another tip and now my code is all messed up... I have to deliver this in the course I’m doing and tried to use strings to support 2 languages... But the way it is, I’d better do it without strings and then do it without kicking. I will take your advice and read the concepts, because I don’t know how it works. Thank you for your help. It is very good to know that the programming "people" help themselves so much. Thanks, hug. When I solve I come back here and put the solution.
– Luís Balmant