从onclick侦听器动态读取EditText得到错误android.widget.CheckBox不能强制转换为and



我创建了一个动态android视图,并在其上添加了复选框和编辑文本,检查以下代码

在此代码中,'list2'的值为{a,b,c....}

List<Integer> checkBoxIdList = new ArrayList<Integer>();
List<Integer> editTextIdList = new ArrayList<Integer>();
    int id  = 0;
    int id1 = 0;
    CheckBox ck;
    TextView txtWt;

        ScrollView sv = new ScrollView(this);
         LinearLayout ll = new LinearLayout(this);
         ll.setOrientation(LinearLayout.VERTICAL);
         sv.addView(ll);
         for(int i=0;i<list2.size();i++)
         {           
             ck = new CheckBox(this);
             ck.setId(id);
             checkBoxIdList.add(id);
             ck.setText(list2.get(i));
             ll.addView(ck);         

             txtWt = new TextView(this);
             txtWt.setText("Weight");
             txtWt.setTag(list2.get(i));
             txtWt.setVisibility(View.GONE);
             ll.addView(txtWt);
             edtWt =  new EditText(this);
             edtWt.setId(id1);
                 editTextIdList.add(id1);
             ll.addView(edtWt);
             id++;
             id1++;
         }
         Button btnSubmit = new Button(this);
         btnSubmit.setText("Submit");
         ll.addView(btnSubmit);
         this.setContentView(sv);

我正试图读取复选框和编辑文本按钮点击的值如下

btnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i = 0; i < checkBoxIdList.size(); i++) {
                    CheckBox chk = (CheckBox)findViewById(checkBoxIdList.get(i));
                    String text = chk.getText().toString();

                }
                for(int i = 0; i < editTextIdList.size(); i++) {
                    try{ 
                        EditText edtWt = (EditText)findViewById(editTextIdList.get(i));
                        String text = edtWt.getText().toString();
                        Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();
                   }
                   catch(Exception e)
                   {
                       Toast.makeText(getApplicationContext(), e.toString(),
                                Toast.LENGTH_SHORT).show();
                       Log.e("pass 1", e.toString());
                   }
                }
            }
        });

在使用这个时,我正确地获得复选框的值为a,b,c。但是在阅读EditText时,我得到了这样的错误

java.lang.ClassCastException: android.widget.CheckBox cannot be cast to android.widget.EditText

但我只在填写EdiText后阅读它。为什么会出现这个错误?我怎么解决它??

如注释中所述,您正在为各种元素设置重复的id。重要的是值,而不是用来设置id的变量。也就是说,最好使用对视图的引用而不是对id的引用。见下文:

List<CheckBox> checkBoxIdList = new ArrayList<CheckBox>();
List<EditText> editTextIdList = new ArrayList<EditBox>();
    CheckBox ck;
    TextView txtWt;

        ScrollView sv = new ScrollView(this);
         LinearLayout ll = new LinearLayout(this);
         ll.setOrientation(LinearLayout.VERTICAL);
         sv.addView(ll);
         for(int i=0;i<list2.size();i++)
         {           
             ck = new CheckBox(this);
             checkBoxIdList.add( ck );
             ck.setText(list2.get(i));
             ll.addView(ck);         

             txtWt = new TextView(this);
             txtWt.setText("Weight");
             txtWt.setTag(list2.get(i));
             txtWt.setVisibility(View.GONE);
             ll.addView(txtWt);
             edtWt =  new EditText(this);
             editTextIdList.add( edtWt );
             ll.addView(edtWt);
         }
         Button btnSubmit = new Button(this);
         btnSubmit.setText("Submit");
         ll.addView(btnSubmit);
         this.setContentView(sv);

然后直接访问:

btnSubmit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i = 0; i < checkBoxIdList.size(); i++) {
                String text = checkBoxIdList.get(i).getText().toString();
            }
            for(int i = 0; i < editTextIdList.size(); i++) {
                try{ 
                    String text = editTextIdList.get(i).getText().toString();
                    Toast.makeText(getApplicationContext(), text,
                    Toast.LENGTH_SHORT).show();
               }
               catch(Exception e)
               {
                   Toast.makeText(getApplicationContext(), e.toString(),
                            Toast.LENGTH_SHORT).show();
                   Log.e("pass 1", e.toString());
               }
            }
        }
    });

最新更新