0

我的应用程序有问题我想插入复选按钮的值但它不起作用,我在单选按钮中遇到了同样的困难,但你帮助我解决了它,我尝试用复选按钮做同样的事情但是它没有按预期进行错误来自方法public void onClick(View view) :

public class MainActivity extends Activity  {
DBAdapter db = new DBAdapter(this); 
CheckBox ct1,ct2,ct3,ct4;
   public void  ajouter(View v) {
       db.insertcc(x,y,z,a,b,c);
       Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
    }

  int x;
  int y;
  int z;
  int a;
  int b;
  int c;

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

    db.open();

    ct1 = (CheckBox)findViewById(R.id.rm_41_1_1);
    ct2 = (CheckBox)findViewById(R.id.rm_41_1_2);
    ct3 = (CheckBox)findViewById(R.id.rm_41_1_3);
}
        public void onClick(View view) {
        boolean checked = ((CheckBox) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.rm_41_1_1:
                if (checked)
                      x=10;
                      Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                    break;
            case R.id.rm_41_1_2:
                if (checked)
                    y=11;
                      Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                    break;
            case R.id.rm_41_1_3:
                if (checked)
                    z=33;
                break;
        }
            Button bton = (Button)findViewById(R.id.ajout_un);
        bton.setOnClickListener(new View.OnClickListener() { 
        @Override
             public void onClick(View v) {
                ajouter(v);
                Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
        }
          });

}

4

1 回答 1

0

在这条线之后

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

这意味着布局内的所有可点击视图都必须是Checkbox为了防止此错误添加此代码

    boolean checked = false;

//Check if View is CheckBox if not will cancel request
    if (view instanceof CheckBox) 
    {
       if (((CheckBox) view).isChecked()) {
               checked = true;
       }
    }else
    {
    return;
    }
于 2013-11-01T18:24:51.353 回答