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
4

3 回答 3

1

you have to define onClickListener() for generating string on button click, Something like this,

    Button btn = (Button)findViewbyId(R.id.yourbuttonid);
    btn.setonclickListener(new onClickListner(){
          public void onclick()
          {
               //write your oncreate code here again
          }
    });
于 2012-05-10T08:13:49.253 回答
0

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);
          }
    });
于 2012-05-10T08:13:13.483 回答
0

You haven't defined an onClickListener for your Button. Do so as follows:

Button button = findViewById(R.id.button);
TextView tv = findViewById(R.id.joke);

button.setOnClickListener(new OnClickListener()  
    public void onClick(View view) {
        String[] myString = getResources().getStringArray(R.array.Jokes); 
        tv.setText(myString[rgenerator.nextInt(myString.length)]);
    }
});
于 2012-05-10T08:17:03.697 回答