使用资源 getResources();在 Android AppWidget 中
我想使用
private static final Random rgenerator = new Random();
Resources res = getResources();
myString = res.getStringArray(R.array.xmlString);
String q = myString[rgenerator.nextInt(myString.length)];
当我在活动中使用相同的内容时,它工作正常,但是如果我在 AppWidget 类中使用,它会在 getResources();
中抛出错误;有什么方法可以在 AppWidget 中使用它,基本上我想要的只是将随机 xml 字符串获取到 appWidget,有什么解决方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AppWidgetProvider,您将在大多数调用中获得上下文对象。使用 context.getResources() 检索您的应用程序资源。
With AppWidgetProvider you will get a context object in most calls. Use context.getResources() to retrieve your application resources.
使用此代码:
Use this code: