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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:47:37+00:00 2026-06-03T16:47:37+00:00

I am trying to get a random string from an array when I press

  • 0

I am trying to get a random string from an array when I press a button.

So far it works with getting a random string when the application starts up, but I cannot get it to work on press of a button after startup.

EDIT:
It now crashes on start up??

My new code:

private String[] myString;
private static final Random rgenerator = new Random();


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



    TextView tv = (TextView) findViewById(R.id.joke);
  Resources res = getResources();

    myString = res.getStringArray(R.array.Jokes); 

    String q = myString[rgenerator.nextInt(myString.length)];    
    tv.setText(q);
    Button btn = (Button)findViewById(R.id.button);


 btn.setOnClickListener(new OnClickListener() { 
public void onClick(View v) {

    myString = res.getStringArray(R.array.Jokes); 

    String q = myString[rgenerator.nextInt(myString.length)];    
    tv.setText(q);
  }
});

}
}

Logcat says:

03-31 22:47:35.777: W/dalvikvm(1152): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-31 22:47:35.777: E/AndroidRuntime(1152): Uncaught handler: thread main exiting due to uncaught exception
03-31 22:47:35.903: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.jc.dkjokes/com.jc.dkjokes.DanskeJokesMain}: java.lang.NullPointerException
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.os.Looper.loop(Looper.java:123)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at dalvik.system.NativeStart.main(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152): Caused by: java.lang.NullPointerException
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.Activity.findViewById(Activity.java:1612)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at com.jc.dkjokes.DanskeJokesMain.<init>(DanskeJokesMain.java:17)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.Class.newInstanceImpl(Native Method)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at java.lang.Class.newInstance(Class.java:1479)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-31 22:47:35.903: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
03-31 22:47:35.903: E/AndroidRuntime(1152):     ... 11 more
03-31 22:47:35.963: I/dalvikvm(1152): threadid=7: reacting to signal 3
03-31 22:47:36.148: I/dalvikvm(1152): Wrote stack trace to '/data/anr/traces.txt'
  • SnoX
  • 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-03T16:47:38+00:00Added an answer on June 3, 2026 at 4:47 pm

    To achieve that you need to declare one button in your main.xml file then you have to initialize that in your activity. Then after you have to set on click event listener on that button.

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:id="@+id/buttonClick"
        />
    

    Code

    TextView tv = (TextView) findViewById(R.id.joke);
    Button btn = (Button)findViewbyId(R.id.buttonClick);
        btn.setonclickListener(new onClickListner(){
              public void onclick()
              {
                   myString =  getResources().getStringArray(R.array.Jokes); 
                   String q = myString[rgenerator.nextInt(myString.length)];
                   tv.setText(q);
              }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a random string from a list of strings in
I am trying to generate random floats using nothing but bytes I get from
i am trying to display a random string each time a button is pressed
What I'm trying to do here is to always get a random value from
I'm trying to get objects from a multi-level JSON array. This is the an
I've struggled with this all day, I am trying to get a random number
Hi im trying to get a uniform random number generator using a crypto algorithm
Hi I am trying to get this script to show random colours one after
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get all html links within a string and replace them using

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.