Android 获取资源();错误
我试图创建一个小部件,但在尝试使用Resources res=getResources()时出错;它说它在widget中未定义,所以我想知道我可以为它做什么。在聊天室里提问会更容易,但这里没有人在安卓聊天室 无论如何,这是我的密码:Android 获取资源();错误,android,Android,我试图创建一个小部件,但在尝试使用Resources res=getResources()时出错;它说它在widget中未定义,所以我想知道我可以为它做什么。在聊天室里提问会更容易,但这里没有人在安卓聊天室 无论如何,这是我的密码: package kevin.erica.box; import java.util.Arrays; import java.util.Random; import android.app.PendingIntent; import android.appwidg
package kevin.erica.box;
import java.util.Arrays;
import java.util.Random;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
Resources res = getResources();
private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();
private String[] myString1;
private String[] myString2,
myString = res.getStringArray(R.array.myArray);
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, Widget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.imagewidgeterica, pendingIntent);
// To update a label
views.setTextViewText(appWidgetId, myString[rgenerator.nextInt(myString.length)]);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
package kevin.erica.box;
导入java.util.array;
导入java.util.Random;
导入android.app.pendingent;
导入android.appwidget.AppWidgetManager;
导入android.appwidget.AppWidgetProvider;
导入android.content.Context;
导入android.content.Intent;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.remoteview;
公共类小部件扩展AppWidgetProvider{
Resources res=getResources();
专用静态最终随机生成器=新随机();
私有静态最终随机rgenerator2=新随机();
私有字符串[]myString1;
私有字符串[]myString2,
myString=res.getStringArray(R.array.myArray);
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
final int N=appWidgetIds.length;
Log.i(“ExampleWidget”,“更新Widget”+Arrays.asList(AppWidgetId));
//对属于此应用程序的每个应用程序小部件执行此循环过程
//提供者
对于(int i=0;i
移动行:
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
要更新onUpdate
方法:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Resources res = context.getResources();
if (myString == null)
{
myString = res.getStringArray(R.array.myArray);
}
// ...
}