3

I want to use

private static final Random rgenerator = new Random();
Resources res = getResources(); 
myString = res.getStringArray(R.array.xmlString); 
String q = myString[rgenerator.nextInt(myString.length)];

When i use the same in an activity it works fine, but if i use in AppWidget class it throws an error in getResources(); is there any way to use this in AppWidget, basically all i want is to get random xml strings to appWidget, is there any workaround for this?

2 Answers 2

8

With AppWidgetProvider you will get a context object in most calls. Use context.getResources() to retrieve your application resources.

0
1

Use this code:

private static final Random rgenerator = new Random();
Resources res = context.getResources();
myString = res.getStringArray(R.array.xmlString); 
String q = myString[rgenerator.nextInt(myString.length)];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.