0

我在开发由问题和多个单选按钮组成的应用程序的最后一部分,我知道如何将单选按钮的每个值分开,但现在我尝试插入单击选中的单选按钮的所有值按钮插入,但我有这个错误:java.lang.ILLegalStateException : could not find a method ajouter (View) in activity class

请问你有什么想法吗?

public class MainActivity extends Activity {

DBAdapter db = new DBAdapter(this); 
  int x=0;
  int y=0;
  int z=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        db.open();

    //  RadioGroup  radio =(RadioGroup)findViewById(rm_25_1);
        RadioButton rad0 = (RadioButton) findViewById (R.id.rm_25_1_1);
        RadioButton rad1 = (RadioButton) findViewById (R.id.rm_25_1_2);

  //      RadioGroup  Télévision  =(RadioGroup)findViewById(rm_25_2);
        RadioButton tel0 = (RadioButton) findViewById (R.id.rm_25_2_1);
        RadioButton tel1 = (RadioButton) findViewById (R.id.rm_25_2_2);

//          RadioGroup  Réfrigérateur =(RadioGroup)findViewById(rm_25_3);
        RadioButton ref0 = (RadioButton) findViewById (R.id.rm_25_3_1);
        RadioButton ref1 = (RadioButton) findViewById (R.id.rm_25_3_2);

}

public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rm_25_1_1:
                    if (checked)
                          x=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_1_2:
                    if (checked)
                        x=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_2_1:
                    if (checked)
                        y=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_2_2:
                    if (checked)
                        y=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_3_1:
                    if (checked)
                        z=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_3_2:
                    if (checked)
                        z=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();

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

          }

          public void  ajouter(View v) {
               db.insertcc(x,y,z);

          }
        });

  }}

这是 XML 文件:

   <TextView
    android:id="@+id/qm_25_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".1.Radio :  "
    />
   <RadioGroup
    android:id="@+id/rm_25_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_1_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="OUI" />

    <RadioButton
        android:id="@+id/ rm_25_1_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="NON" />

</RadioGroup>

   <TextView
    android:id="@+id/qm_25_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".2. Télévision :"
    />
   <RadioGroup
    android:id="@+id/rm_25_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_2_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="Oui" />

    <RadioButton
        android:id="@+id/ rm_25_2_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="Non" />

</RadioGroup>

    <TextView
    android:id="@+id/qm_25_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".3. Réfrigérateur :"
    />
   <RadioGroup
       android:id="@+id/rm_25_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_3_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="Oui" />

    <RadioButton
        android:id="@+id/rm_25_3_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="Non" />
</RadioGroup>
<Button
    android:id="@+id/ajout_un"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="ajouter"
    android:layout_marginTop="26dp"
    android:layout_marginLeft="150dp"
    android:text="ENREGISTRER"
    android:textStyle="bold" />
4

2 回答 2

0

尝试将ajouter(v);call 更改为this.ajouter(v);. this应该参考您的 OnClickListener 类。

于 2013-10-31T12:27:03.700 回答
0

将您的 ajouter() 方法从单击侦听器类中取出,并将其作为您活动的私有成员。

于 2013-10-31T12:39:42.687 回答