我以前做过。这不是火箭科学。一个包含完美形式的美丽活动。一个EditText
这里……一个Spinner
那里。但这次有CheckBox
'es。
这应该很简单:设置onClick()
XML 中声明的方法,获取视图的 ID...
...事情是,我一直在使用 aBundle
将表单中的所有数据收集在一起并发送给我IntentService
以放入 SQLite 数据库。
/**
* Called when the user changes the state of any CheckBox
* @param view The View that was checked/unchecked */
public void onCheckBoxChng(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
String mField = new String();
// Check which checkbox was clicked
switch(view.getId()){
case R.id.dlg_add_ply_chk1:
mField = "Platinum";
break;
case R.id.dlg_add_ply_chk2:
mField = "Gold";
break;
case R.id.dlg_add_ply_chk3:
mField = "Silver";
break;
case R.id.dlg_add_ply_chk4:
mField = "Bronze";
break;
case R.id.dlg_add_ply_chk5:
mField = "HQ";
break;
case R.id.dlg_add_ply_chk6:
mField = "Aurora-1";
break;
}
if (checked) {
setBundleChk(mField,1);
} else {
setBundleChk(mField,0);
}
}
/**
* Set the boolean fields in mBdlgForm based on user action on the checkboxes
*
* @param Field The field name to change
* @param State The state to set (<code>true</code> or <code>false</code> */
private void setBundleChk(String Field, int State){
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_ACTDLGADDPLYR,
"setBundleChk(Field, State) called with: "+Field+", and "+State);
Log.i(Constants.TAG_ACTDLGADDPLYR, "setBundleChk(): mBdlgForm Keys :");
for (String key: mBdlgForm.keySet()){
Log.d (Constants.TAG_FRGACTPLAYERS, "mBdlgForm."+key);
}
}
if (State == 1) {
mBdlgForm.putBoolean(Field, true);
} else {
mBdlgForm.putBoolean(Field, false);
}
}
小菜一碟,对吧?
问题是,虽然Bundle
' 范围是整个活动,但在我从回调调用的任何函数中的 'es NOR的onClick()
回调中都看不到它......CheckBox
onClick()
...请告诉我...我错过了什么?
即使在实施“Activity.this.method()”之后,我仍然会收到以下 Logcat 消息:
03-04 20:09:32.349: E/AndroidRuntime(641): FATAL EXCEPTION: main
03-04 20:09:32.349: E/AndroidRuntime(641): java.lang.IllegalStateException: Could not execute method of the activity
03-04 20:09:32.349: E/AndroidRuntime(641): at android.view.View$1.onClick(View.java:3591)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.view.View.performClick(View.java:4084)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.widget.CompoundButton.performClick(CompoundButton.java:100)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.view.View$PerformClick.run(View.java:16966)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.os.Handler.handleCallback(Handler.java:615)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.os.Looper.loop(Looper.java:137)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-04 20:09:32.349: E/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 20:09:32.349: E/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 20:09:32.349: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-04 20:09:32.349: E/AndroidRuntime(641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-04 20:09:32.349: E/AndroidRuntime(641): at dalvik.system.NativeStart.main(Native Method)
03-04 20:09:32.349: E/AndroidRuntime(641): Caused by: java.lang.reflect.InvocationTargetException
03-04 20:09:32.349: E/AndroidRuntime(641): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 20:09:32.349: E/AndroidRuntime(641): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 20:09:32.349: E/AndroidRuntime(641): at android.view.View$1.onClick(View.java:3586)
03-04 20:09:32.349: E/AndroidRuntime(641): ... 12 more
03-04 20:09:32.349: E/AndroidRuntime(641): Caused by: java.lang.NullPointerException
03-04 20:09:32.349: E/AndroidRuntime(641): at net.clmitchell.ewtraker.pPlayers.ActDlgAddPlyr.setBundleChk(ActDlgAddPlyr.java:260)
03-04 20:09:32.349: E/AndroidRuntime(641): at net.clmitchell.ewtraker.pPlayers.ActDlgAddPlyr.onCheckBoxChng(ActDlgAddPlyr.java:231)
03-04 20:09:32.349: E/AndroidRuntime(641): ... 15 more
最后标识的两行分别是:
if (checked){ActDlgAddPlyr.this.setBundleChk(mField,1);}
for (String key: ActDlgAddPlyr.this.mBdlgForm.keySet()){
下面是第二种方法:
/**
* Set the boolean fields in mBdlgForm based on user action on the checkboxes
*
* @param Field The field name to change
* @param State The state to set (<code>true</code> or <code>false</code> */
private void setBundleChk(String Field, int State){
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_ACTDLGADDPLYR, "setBundleChk(Field, State) called with: "+Field+", and "+State);
Log.i(Constants.TAG_ACTDLGADDPLYR, "setBundleChk(): mBdlgForm Keys :");
for (String key: ActDlgAddPlyr.this.mBdlgForm.keySet()){
Log.d (Constants.TAG_FRGACTPLAYERS, "mBdlgForm."+key);
}
}
if(State == 1){ActDlgAddPlyr.this.mBdlgForm.putBoolean(Field, true);}
else{ActDlgAddPlyr.this.mBdlgForm.putBoolean(Field, false);}
}