Android Studio - CheckBox让App崩溃

时间:2016-01-25 01:29:30

标签: android checkbox crash

我目前正在处理一个注册页面的活动,您可以在其中输入您的信息并将其存储在一个对象中。

当我尝试在我的代码中实现复选框时出现了这个问题。

有解决方法吗?

public class Register extends AppCompatActivity implements View.OnClickListener {
    Button bRegister, bBack;
    EditText etName, etAge, etUsername, etPassword;
    CheckBox cbAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // Get user info
        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassowrd);
        bRegister = (Button) findViewById(R.id.bRegister);
        bBack = (Button) findViewById(R.id.bBack);
        cbAccount = (CheckBox)findViewById(R.id.cbAccount);

        bRegister.setOnClickListener(this);
        bBack.setOnClickListener(this);
        cbAccount.setOnClickListener(this);
    }

    boolean account = false;

    @Override
    public void onClick(View view) {
        boolean checked = ((CheckBox) view).isChecked();
        //"switch" is like an if statement, but one can put multiple "cases"
        switch(view.getId()) { 
            // When user registers, store info
            case R.id.bRegister:
                // Store user info
                String name = etName.getText().toString();
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                int age = Integer.parseInt(etAge.getText().toString());
                boolean accountSend = account;
                // Call method to store user info to Login.java class for authentication
                userLocalStorage uls = userLocalStorage.getInstance();

                uls.setUsername(username);
                uls.setPassword(password);
                uls.setName(name);
                uls.setAge(age);
                uls.setAccount(accountSend);

                startActivity(new Intent(this, Login.class));

                break;
            case R.id.bBack:
                startActivity(new Intent(this, Login.class));
                break;
            case R.id.cbAccount:
                if (checked) {
                    account = true;
                } else {
                    account = false;
                }
                break;
        }
    }
}

这是logcat
01-24 20:43:15.971 8074-8074 / com.example.huynh.nom E / AndroidRuntime:致命例外:主要
                                                                     处理:com.example.huynh.nom,PID:8074
                                                                     java.lang.ClassCastException:android.support.v7.widget.AppCompatButton无法强制转换为android.widget.CheckBox
                                                                         在com.example.huynh.nom.Register.onClick(Register.java:44)
                                                                         在android.view.View.performClick(View.java:5198)
                                                                         在android.view.View $ PerformClick.run(View.java:21147)
                                                                         在android.os.Handler.handleCallback(Handler.java:739)
                                                                         在android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         在android.os.Looper.loop(Looper.java:148)
                                                                         在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:0)

@Override
public void onClick(View view) {
    boolean checked = ((CheckBox) view).isChecked();

只要点击bRegisterbBackcbAccount,就会调用此方法,因为您在此处设置点击侦听器:

    bRegister.setOnClickListener(this);
    bBack.setOnClickListener(this);
    cbAccount.setOnClickListener(this);

但是,其中只有一个实际上是CheckBox,所以当点击其他两个中的任何一个时,会导致错误的演员。

因此,请将boolean checked = ((CheckBox) view).isChecked();移至case R.id.cbAccount:

答案 1 :(得分:0)

onClick处理程序中,您有以下行:

boolean checked = ((CheckBox) view).isChecked();

由于您注册了同一个处理程序以供多个不同的视图使用,因为它们不是所有的CheckBox都不起作用。

删除该行,您可以改为:

case R.id.cbAccount:
    if (cbAccount.isChecked()) {
        account = true;
    } else {
        account = false;
    }
break;

答案 2 :(得分:0)

我发现这个问题:java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.CheckBo x,您可以检查布局文件并确保使用正确的小部件。