Random String from array.

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Random String from array.

Postby zaid » Tue Apr 27, 2010 8:58 am

i am trying to create a button that when clicked on will choose a random string from a defined array and display that string.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     String[] myString;
  2.  
  3.  
  4.  
  5.     Resources res = null;
  6.  
  7.         myString = res.getStringArray(R.array.myArray);
  8.  
  9.  
  10.  
  11.     String q = myString[rgenerator.nextInt(myString.length)];
  12.  
  13. TextViewstringhere.setText(q)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


this is my array.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <resources>
  5.  
  6. <string-array name="myArray">
  7.  
  8.     <item>string 1</item>
  9.  
  10.     <item>string 2</item>
  11.  
  12.     <item>string 3</item>
  13.  
  14.     <item>string 4</item>
  15.  
  16.     <item>string 5</item>
  17.  
  18. </string-array>
  19.  
  20. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


this however cause my application to crash in the emulator and force close.
zaid
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 27, 2010 8:41 am

Top

Postby stealthcopter » Tue Apr 27, 2010 10:29 am

Look at your code, you are setting resources to null, and them attempting to get a string from it.

I have already answered this question on stackoverflow here and added the example code nessecary for you to understand it.
stealthcopter
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Jan 17, 2010 11:42 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 11 guests