0

クリックされたボタンに基づいて異なるアクションを実行するリスナーの設定に関して問題があります。

コンパイラは構文上のエラーを検出しませんが、問題は、エミュレータでアプリをシミュレートすると、「強制終了」を求めるウィンドウがポップアップすることです。

ここに私のJavaファイルがあります:

package tp.imc.namespace;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class IMCCalculatorActivity extends Activity {
   EditText poids,taille;
   Button boutton1,boutton2;
   TextView text1;
   Boolean k=true;
@Override

public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
    poids=(EditText) findViewById(R.id.poids);
    taille=(EditText) findViewById(R.id.taille);
    boutton1 =(Button) findViewById(R.id.boutton1);
    text1=(TextView) findViewById(R.id.text1);
    boutton1.setOnClickListener(clicker);
    boutton2.setOnClickListener(clicker);
}

// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener clicker = new View.OnClickListener(){
    public void  onClick  (View  v){
        if( v == boutton1 )
{
            String a,b;
    Double r;
    a=poids.getText().toString();
    b=taille.getText().toString();
    Double zero=Double.parseDouble(b);

    a=poids.getText().toString();
    b=taille.getText().toString();

    if (zero==0)
     {
    text1.setText("Erreur ! Division par 0.");

     }
    else {
        r=(Double.parseDouble(a))/(Double.parseDouble(b)*Double.parseDouble(b));
        if (r<16.5) text1.setText("Famine.");
        else if (r>16.5 && r<18.5) text1.setText("Maigreur.");
        else if (r>=18.5 && r<25) text1.setText("Coplulence nomrale.");
        else if (r>=25 && r<30) text1.setText("Surpoids.");
        else if (r>=30 && r<35) text1.setText("Obésité modérée.");
        else if (r>=35 && r<40) text1.setText("Obésité sévère.");
        else if (r>=40) text1.setText("Obésité morbide ou massive.");

         }

}           
        else if( v == boutton2 )

        {
             poids.setText("");
     taille.setText("");
     text1.setText("");
        }                          
}
 };
}

そして、ここに私のXMLファイルがあります:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/poids"
    android:textColor="#FF0000"
    android:gravity="center"

    />

<EditText
    android:id="@+id/poids"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/poids"
    android:lines="1"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/taille"
    android:textColor="#FF0000"
    android:gravity="center"
   />

<EditText
    android:id="@+id/taille"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/taille"
    android:lines="1" 
     />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  
    android:checked="true" 
    android:text="@string/metre"
    />
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/centimetre"
     />

</RadioGroup>

<CheckBox 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mega"
    android:checked="false"

    />


<Button
    android:id="@+id/boutton1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/compute" />

<Button
    android:id="@+id/boutton2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/raz" />
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/resultat"
    />
<TextView  
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="@string/text"
    />
</LinearLayout>
4

1 に答える 1

4

を定義しませんboutton2。null です。これを追加:

boutton2 = (Button) findViewById(R.id.boutton2);
boutton2.setOnClickListener(clicker);

boutton1boutton2の間には共通のコードがないため、単純に 2 つの異なるリスナーを作成できます。

最後に、この行は正しくありません。

if( v == boutton1 )

ビューとボタンを比較しようとしていますが、機能しない可能性があります。これを試して:

if( v.getId() == boutton1.getId() )
于 2012-06-06T19:01:21.667 に答える