android.widget.Button无法强制转换为android.widget.CheckBox

时间:2016-06-16 08:50:18

标签: java android checkbox

我在Android项目中从标题中获取错误。我已经搜索过类似的问题,这些问题让我进行了复选框投射,这似乎没问题,还可以清理和重建项目,但它不起作用。

当我点击"注册"应用程序崩溃包含EditTexts(用于用户名,电子邮件和密码)和复选框的注册表单后面的按钮,供用户选择多个项目。接下来我该怎么办?

public class RegisterActivity extends Activity {

ArrayList<Sport> sports = new ArrayList<>();
ArrayList<Sport> selectedSports = new ArrayList<>();

private void initializeSports(){
    String[] sportsArray = getResources().getStringArray(array.sportsName);

    for (int i = 0; i < sportsArray.length; i++){
        sports.add(new Sport(i + 1, sportsArray[i]));
    }


}

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

    initializeSports();


    final EditText etEmail = (EditText) findViewById(id.etEmail);
    final EditText etUsername = (EditText) findViewById(id.etUsername);
    final EditText etPassword = (EditText) findViewById(id.etPassword);

    final Button bRegister = (Button) findViewById(id.bRegister);

    final CheckBox cbSoccer = (CheckBox) findViewById(id.cbSoccer);
    cbSoccer.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });

    final CheckBox cbFootball = (CheckBox) findViewById(id.cbFootball);
    cbFootball.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });

    final CheckBox cbBasket = (CheckBox) findViewById(id.cbBasket);
    cbBasket.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });
    final CheckBox cbTennis = (CheckBox) findViewById(id.cbTennis);
    cbTennis.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });
    final CheckBox cbSwimming = (CheckBox) findViewById(id.cbSwimming);
    cbSwimming.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });
    final CheckBox cbGym = (CheckBox) findViewById(id.cbGym);
    cbGym.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });
    final CheckBox cbOther = (CheckBox) findViewById(id.cbOther);
    cbOther.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            selectItem(v);
        }
    });


    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String username = etUsername.getText().toString();
            final String email = etEmail.getText().toString();
            final String passcode = etPassword.getText().toString();

            selectItem(v);

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        if (success) {
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(username, email, passcode, selectedSports, responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}

public void selectItem(View v){

    boolean checked = ((CheckBox)v).isChecked();
    switch(v.getId()){

        case R.id.cbSoccer:

            if(checked){
                selectedSports.add(sports.get(0));


            } else{
                selectedSports.remove(sports.get(0));
    } break;

        case R.id.cbFootball:

            if(checked){
                selectedSports.add(sports.get(1));
            } else{
                selectedSports.remove(sports.get(1));
            } break;

        case R.id.cbTennis:

            if(checked){
                selectedSports.add(sports.get(2));

            } else{
                selectedSports.remove(sports.get(2));
            } break;

        case R.id.cbSwimming:

            if(checked){
                selectedSports.add(sports.get(3));


            } else{
                selectedSports.remove(sports.get(3));
            } break;

        case R.id.cbGym:

            if(checked){
                selectedSports.add(sports.get(4));


            } else{
                selectedSports.remove(sports.get(4));
            } break;

        case R.id.cbBasket:

            if(checked){
                selectedSports.add(sports.get(5));


            } else{
                selectedSports.remove(sports.get(5));
            } break;

        case R.id.cbOther:

            if(checked){
                selectedSports.add(sports.get(6));

            } else{
                selectedSports.remove(sports.get(6));
            } break;
    }
}

}

3 个答案:

答案 0 :(得分:0)

您正在selectItem(v)方法和侦听器中将视图转换为CheckBox

bRegister.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        selectItem(v);
    }
});

您正在传递View v,即单击的按钮,因此该按钮无法转换为CheckBox。

从侦听器中删除此行

selectItem(v);

它会没事的。

答案 1 :(得分:0)

selectItem(v)方法中,传递给该方法的视图是Button的一个实例,因为它是在按钮的onClickListener中编写的

如果您想要做同样的事情,请首先检查selectItem(View v)方法

if(v instanceof CheckBox) {
      boolean checked = ((CheckBox)v).isChecked();
}

然后继续使用剩下的代码

答案 2 :(得分:-1)

你有

bRegister.setOnClickListener(new View.OnClickListener() {
    ...
    selectItem(v);
    ...
}

selectItem方法内部有行

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

这是一个类型广告 - ViewOnClickListener设置为Button而不是instanceof CheckView