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!
Very odd. I’m doing the exact same thing in one of my programs without issue:
And my the portion from my res/values/strings.xml:
The only thing I can think is that your R.java file is corrupt in some way.
Try this:
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.