0

我有一个带有两个单选按钮(自我交付和公司交付)的单选组。在用户选择任何一个单选按钮时的申请表中。我正在获取单选按钮的文本并使用此代码插入数据库。

表单.java

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
             selectedId = radioGroup.getCheckedRadioButtonId();

                // find the radiobutton by returned id
                    radioButton = (RadioButton) findViewById(selectedId);


                    if(radioButton.isChecked()){
                         OPTIONS=radioButton.getText().toString();
                        if(OPTIONS.equals("SelfDelivery")){
                            deliveryAddress.setVisibility(View.GONE);
                            deliveryEdit.setVisibility(View.GONE);  


                        }else{
                            deliveryAddress.setVisibility(View.VISIBLE);
                            deliveryEdit.setVisibility(View.VISIBLE);   
                        }
                    }
            }
        });

当用户想要编辑表单时,我正在创建新屏幕以编辑和更新 details.by 使用代码。

EditForm.java

final String ram=extras.getString(openHelper_ob.DELIVERY_OPTIONS);// getting the option from Form.java to EditForm.java

        OPTIONS=ram;

        if(OPTIONS.equals("CompanyDelivery")){
        radioButtonCompany.setChecked(true);
            deliveryText.setVisibility(View.VISIBLE);
            editAddress.setVisibility(View.VISIBLE);
        }else if(OPTIONS.equals("SelfDelivery")){
            radioButtonSelf.setChecked(true);
            deliveryText.setVisibility(View.GONE);
            editAddress.setVisibility(View.GONE);
        }



         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup arg0, int arg1) {
                    // TODO Auto-generated method stub
                 selectedId = radioGroup.getCheckedRadioButtonId();

                    // find the radiobutton by returned id
                        radioButton = (RadioButton) findViewById(selectedId);


                        if(radioButton.isChecked()){
                             OPTIONS=radioButton.getText().toString();
                            if(OPTIONS.equals("SelfDelivery")){
                                deliveryText.setVisibility(View.GONE);
                                editAddress.setVisibility(View.GONE);   


                            }else{
                                deliveryText.setVisibility(View.VISIBLE);
                                editAddress.setVisibility(View.VISIBLE);    
                            }
                        }
                }
            });

我想在返回编辑页面时根据用户选择使单选按钮为真。例如:如果用户选择了公司送货,当用户在送货选项中编辑表格时,应在编辑页面中检查公司送货。

我收到错误

radioButtonCompany.setChecked(true);

错误是

10-10 04:41:20.721: E/AndroidRuntime(794): FATAL EXCEPTION: main
10-10 04:41:20.721: E/AndroidRuntime(794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.EditBookingForm}: java.lang.NullPointerException
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.os.Looper.loop(Looper.java:137)
10-10 04:41:20.721: E/AndroidRuntime(794):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-10 04:41:20.721: E/AndroidRuntime(794):  at java.lang.reflect.Method.invokeNative(Native Method)
10-10 04:41:20.721: E/AndroidRuntime(794):  at java.lang.reflect.Method.invoke(Method.java:511)
10-10 04:41:20.721: E/AndroidRuntime(794):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-10 04:41:20.721: E/AndroidRuntime(794):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-10 04:41:20.721: E/AndroidRuntime(794):  at dalvik.system.NativeStart.main(Native Method)
10-10 04:41:20.721: E/AndroidRuntime(794): Caused by: java.lang.NullPointerException
4

1 回答 1

0

在没有 Logcat 打印输出的情况下,我只能假设您没有在 radioButtonCompany 的布局中找到合适的 id 并且收到空指针错误。

或者,您正试图在 UI 线程之外修改单选按钮的检查状态,因此需要将您的 setChecked 包含在 runOnUIThread( ) { } 子句中。但是额外的 LogCat 输出将有很大帮助。

于 2013-10-09T14:35:09.327 回答