Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8041345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:11:03+00:00 2026-06-05T04:11:03+00:00

I have a problem concerning setting a listener that would execute different action based

  • 0

I have a problem concerning setting a listener that would execute different action based on the clicked button.

The compiler doesn’t detect any error on the syntax, but the problem is that when I simulate my app on the emulator, it pops up the window asking for “Force Close”.

Here is my Java file :

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

And here is my XML file :

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:11:04+00:00Added an answer on June 5, 2026 at 4:11 am

    You don’t define boutton2, it is null. Add this:

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

    Since boutton1 and boutton2 don’t have common code between them you could simply create two different listeners.

    Lastly, this line doesn’t look right.

    if( v == boutton1 )
    

    You are trying to compare a View to a Button which may not work. Try this:

    if( v.getId() == boutton1.getId() )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem concerning a select query in MYSQL i have two different
I have a problem, concerning the jdk/jre usage of my eclipse. I know that
I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem concerning the width of a DialogPreference in Android. According to
I am using LaTeX and I have a problem concerning string manipulation. I want
i am currently working on a facebook game and i have a problem concerning
I have a huge problem concerning png images. My png is a black/white letter
I have quite a problem concerning the use of relational database concepts in Delphi
I'm a beginner concerning socket programming and I have a problem when I send
I'm writing a C# DKIM validator and have come across a problem that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.