Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:23:53+00:00 2026-06-03T01:23:53+00:00

Im trying to create a widget and i get a error when trying to

  • 0

Im trying to create a widget and i get a error when trying to use the Resources res = getResources(); It says its undefined in widget so i’d like to know what i could sub it for. It would be easier to ask in chat but no one is in the android chat on here

Anyway heres my code:

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);
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:23:55+00:00Added an answer on June 3, 2026 at 1:23 am

    Move the lines:

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

    To the onUpdate method:

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        Resources res = context.getResources();
        if (myString == null)
        {
            myString = res.getStringArray(R.array.myArray);
        }
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a widget within the module and then load that widget
I am trying to create a widget in android in which when I click
I'm trying to create an icon/widget (1 cell x 1 cell) that can be
I am trying to create a simple android widget that shows The time but
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a background-image slideshow and am getting this error... This is the
I'm a jQuery newb and I've been trying to create a custom slideshow widget
I'm trying to use a widget within a plugin in wordpress and I'm seeing
I'm trying to use the 'edge.create' callback function of the Facebook javascript SDK to
I'm trying to use the button widget of jquery-ui in my ror project. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.