dimanche 24 janvier 2016

Android Studio - CheckBox is making App crash

I am currently working on an activity for a register page where you input your info and it would store it in an object.

This problem arose when I tried to implement Check boxes into my code.

Is there a solution for this?

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;
        }
    }
}

Here is the logcat
01-24 20:43:15.971 8074-8074/com.example.huynh.nom E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.huynh.nom, PID: 8074
java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.CheckBox
at com.example.huynh.nom.Register.onClick(Register.java:44)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)




Aucun commentaire:

Enregistrer un commentaire