2012-05-02 5 views
0

私はウィジェットを作成しようとしていますが、リソースres = getResources();を使用しようとするとエラーが発生します。それはウィジェットで定義されていないので、私は何のためにそれを補助することができるかを知りたいと思います。チャットで質問して容易になるだろうが、誰もとにかくここにアンドロイドチャットでHERESに私のコードgetResources();エラー

ではありません:

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); 
    } 
    } 
} 

答えて

0

移動ライン:onUpdate方法に

Resources res = getResources(); 
myString = res.getStringArray(R.array.myArray); 

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{ 
    Resources res = context.getResources(); 
    if (myString == null) 
    { 
     myString = res.getStringArray(R.array.myArray); 
    } 
    // ... 
} 
関連する問題