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