0

我无法返回值。单击同意按钮时应用程序强制关闭。我无法从 i_am.isChecked() 获取所有其他单选按钮的值。

protected void onCreate(Bundle savedInstanceState) {
    final RadioButton i_am,practic,read_terms;
    i_am=(RadioButton)findViewById(R.id.first);
    practice=(RadioButton)findViewById(R.id.second);
    read_terms=(RadioButton)findViewById(R.id.third);

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.termandconditionxml);

    Button agree_button=(Button)findViewById(R.id.Agree);
    agree_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(checker()) {
                Toast.makeText(getApplicationContext(), "done all cliked",Toast.LENGTH_LONG).show();
            }
            else {
// TODO Auto-generated method stub
                new AlertDialog.Builder(Termsandcondition.this)
                .setTitle("Error")
                .setMessage("Please verify ")
                .setPositiveButton("OK",null).show();

            }
        }

        private boolean checker() {
            // TODO Auto-generated method stub
            System.out.println("iamanhcp"+i_am.isChecked());
            if(i_am.isChecked()) {
                System.out.println("iahcp"+i_am.isChecked());
                if(practice.isChecked()) {  
                    System.out.println("iama"+practice.isChecked());    
                    if(read_terms.isChecked()) {    
                        System.out.println("iama"+read_terms.isChecked());
                        return true;    
                    }
                }
            }

            return false;
        }


    });
}//oncreate end

==============================

07-05 15:36:14.301: W/dalvikvm(1702): threadid=1: thread exiting with uncaught exception (group=0x40014760)
07-05 15:36:14.332: E/AndroidRuntime(1702): FATAL EXCEPTION: main
07-05 15:36:14.332: E/AndroidRuntime(1702): java.lang.NullPointerException
07-05 15:36:14.332: E/AndroidRuntime(1702):     at com.andoid.Termsandcondition$1.checker(Termsandcondition.java:53)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at com.andoid.Termsandcondition$1.onClick(Termsandcondition.java:36)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.view.View.performClick(View.java:3110)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.view.View$PerformClick.run(View.java:11934)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.os.Handler.handleCallback(Handler.java:587)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.os.Looper.loop(Looper.java:132)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at android.app.ActivityThread.main(ActivityThread.java:4123)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at java.lang.reflect.Method.invoke(Method.java:491)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-05 15:36:14.332: E/AndroidRuntime(1702):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

可能是因为你getApplicationContext在你的Toast. 改为使用this。要从您RadioButton的 s 中获取值,请使用OnCheckedChangeListener.

    Toast.makeText(this, "done all cliked",Toast.LENGTH_LONG).show();

    i_am.setOnCheckedChangeListener(this);

希望这对您有所帮助。

于 2012-07-10T10:05:03.643 回答