Why can't I use Resources.getSystem() without a Runtime error?
According to Android documentation, Resources.getSystem()
only provides system-level resources, not application-level ones (like the resources inside your strings.xml file).
http://developer.android.com/reference/android/content/res/Resources.html#getSystem()
Try using the application's context if you really want to retrieve your strings this way, or take my suggestion in the comment to your question.
Android Studio throwing android.content.res.Resources$NotFoundException
You're referencing the string array in a wrong way. Replace the Resources.getSystem()
with getActivity().getResources()
if you're in a fragment, but if the code is in an activity, simply call getResources()
directly. Finally your code should look like this ...getResources().getStringArray(...);
/
How to use string resources without error?
As @Micheal pointed out, I used Resources.getSystem() to get strings from resources. Instead I should have used a context to call getResources(). A simple fix should look like this.
String alertMessage = Resources.getSystem().getString(R.string.AddWordAlertDialogMessage, word); // error
String alertMessage = context.getResources().getString(R.string.AddWordAlertDialogMessage, word);
Resources.getSystem().getString vs just placing a string
Resources.getSystem()
references to the system resources and not your app specific resource. Based on the documentation:
Return a global shared Resources object that provides access to only
system resources (no application resources), and is not configured for
the current screen (can not use dimension units, does not change based
on orientation, etc).
So Resources.getSystem().getString(R.string.vote_count)
would search in system only and not in your app. It won't find it since there is no String with id vote_count
in the system.
use context.getResources().getString(R.string.vote_count)
or context.getString(R.string.vote_count)
instead.
Getting String Array in Android from .xml using Resources.getSystem()
One suggestion, not sure if it will work. But you can try and let me know. Why not get the context in QuestionM constructor and initialize your class level context variable with the received context. Now use this context to
mContext.getResources().getStringArray(R.array.array_name);
public class QuestionM {
private String mQuestion;
private String mAnswer;
private String mExplanation;
private Context mContext;
//constructor
public QuestionM(String question,String explanation, Context context) {
mQuestion = question;
mExplanation = explanation;
mContext = context;
}
public class QuestionnaireM {
private List<QuestionM> mQuestionsList;
//constructor
public QuestionnaireM(){
mQuestionsList = new ArrayList<>();
//when i creating object of that class android points the crush here
String [] questions = mContext.getResources().getStringArray(R.array.test);
String [] questionExplanations = mContext.getResources().getStringArray(R.array.test_a);
for (int i=0; i<questions.length; i++){
QuestionM question = new QuestionM(questions[i],questionExplanations[i]);
mQuestionsList.add(question);
}
}
Android : String resource not found while trying to get resource in List activity class
I think there are 2 problems :
You should not access the resources in the constructor or in field initializers, it is too early. The right place is in the
onCreate
methods.Resources.getSystem().getString()
can only find system resources, not the one you define in your application. You should use getString from the Activity.
In your case :
public class RestaurantList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restos);
RestTemplate restTemplate = StaticRestTemplate.getRest();
String restaurantList = getString(R.string.baseUrl) + "restaurant/listing";
}
Related Topics
Android Buildscript Repositories: Jcenter VS Mavencentral
Android Studio - Failed to Complete Gradle Execution - Error in Opening Zip File
Android Runtime Permissions- How to Implement
Android Drawable Speech Bubble
Theme.Appcompat.Light.Darkactionbar - No Resource Found
Detecting Device Type in a Web Application
How Is String Concatenation Implemented in Java 9
Why Does a Try/Catch Block Create New Variable Scope
Transformexception Duplicate Entry for Common.Annotations.Beta
Online Radio Streaming App for Android
Divide Elements on Groups in Recyclerview or Grouping Recyclerview Items ,Say by Date
Multiple Typeface in Single Textview
How to Override Action Bar Back Button in Android