使用资源 getResources();在 Android AppWidget 中

发布于 2024-12-11 01:46:27 字数 392 浏览 3 评论 0原文

我想使用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˇ宁静的妩媚 2024-12-18 01:46:28

使用 AppWidgetProvider,您将在大多数调用中获得上下文对象。使用 context.getResources() 检索您的应用程序资源。

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

惟欲睡 2024-12-18 01:46:28

使用此代码:

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

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)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文