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 7537161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:46:26+00:00 2026-05-30T06:46:26+00:00

I’m trying to make a real simple app in which clicking a button has

  • 0

I’m trying to make a real simple app in which clicking a button has a Toast pop up which displays a random message from an array. The problem isn’t that I can’t get it to work at all, it’s that I can get it to work by creating and listing the array in the activity, but not any of the other ways I’ve read about. So, this code works fine:

package squatingyeti.loveyou.android;


import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.util.Random;

public class LoveYouActivity extends Activity {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(btnListener);


    }

    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            showThem();
        }
    };

    private static final Random RANDOM = new Random();

    /**Resources res = getResources();
    String[] love = res.getStringArray(R.array.love_array);
    */


    private static final String[] love = {
        "testone",
        "testtwo",
        "testthree",
        "testfour",
        "testfive",
        "testsix",
        "testseven"
    }; 

    private void showThem() {
        int showLength = love.length;
        String rsn = love[RANDOM.nextInt(showLength)];
        Toast.makeText(getBaseContext(), rsn, Toast.LENGTH_LONG).show();

    }
}

You can see where I commented out the other way I attempt to use the array. If I comment out the array created in the activity and use the “love = res.getStringArray…” portion of the code, the emulator force closes the app. I can start it again, but it loads with a black screen, no button and just the app name at top. I’m not sure why attempting to call the String[] from the arrays.xml file causes this vs. creating the array and writing out each item in the actual activity. Here is the arrays.xml in my values folder for when I use the method that is failing:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="love_array">
        <item>testone</item>
        <item>testtwo</item>
        <item>testthree</item>
        <item>testfour</item>
        <item>testfive</item>
    </string-array>
</resources>

Lastly, here is the R.java portion pertaining to the array:

public final class R {
public static final class array {
    public static final int love_array=0x7f050000;
}

I mean, this seems really stupid as the app serves no serious purpose other than my messing around and I can make it work. I was just trying to figure out why I can’t make it work the other way when I’ve read that is the correct way.

Thanks!

  • 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-05-30T06:46:27+00:00Added an answer on May 30, 2026 at 6:46 am

    Very odd. I’m doing the exact same thing in one of my programs without issue:

    String[] tableItems = getResources().getStringArray(R.array.settings_menulist_array);
    

    And my the portion from my res/values/strings.xml:

    <string-array name="settings_menulist_array">
        <item>Login ID</item>
        <item>Password</item>
        <item>Menu item #3</item>
        <item>Menu Item #4</item>
    </string-array>
    

    The only thing I can think is that your R.java file is corrupt in some way.
    Try this:

    1. Remove the app from the device/emulator.
    2. Delete R.Java
    3. Perform a Clean (assuming you’re using Eclipse, then Project -> Clean…)

    See if that fixes the issue.

    * Updated based on testing *
    Move the assignment into a method – ie, onResume(). getResources() returns null when outside of a function – this will fix your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.