当我按下按钮时,我试图从数组中获取随机字符串。
到目前为止,它适用于在应用程序启动时获取随机字符串,但在启动后按下按钮无法使其工作。
编辑: 它现在在启动时崩溃了吗?
我的新代码:
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说:
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'
答案 0 :(得分:1)
你必须定义onClickListener()以在按钮点击时生成字符串,像这样,
Button btn = (Button)findViewbyId(R.id.yourbuttonid);
btn.setonclickListener(new onClickListner(){
public void onclick()
{
//write your oncreate code here again
}
});
答案 1 :(得分:0)
要实现这一点,您需要在main.xml文件中声明一个按钮,然后您必须在活动中初始化该按钮。然后,您必须在该按钮上设置单击事件侦听器。
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/buttonClick"
/>
<强>代码强>
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);
}
});
答案 2 :(得分:0)
您尚未为onClickListener
定义Button
。请按以下步骤操作:
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)]);
}
});