0

the last question by me that was answered really help me. Thanks for all that. But now i am getting a new error. This is my code:

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;

public class Trial extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View b1 = findViewById(R.id.button1);
        b1.setOnClickListener(yourListener);  
        View b2 = findViewById(R.id.button2);
        b1.setOnClickListener(yourListener);  
    }
    View.OnClickListener yourListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == button1) {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("Paracettamol")
                        .setMessage(
                                "This medicine is generally used to cure Fever")
                        .setNeutralButton("OK", null).show();
            } else if (v == button2) {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("sertraline")
                        .setMessage(
                                "This medicine is generally used to cure Head aches")
                        .setNeutralButton("OK", null).show();
            }


        }
    };
}

Firstly, i would like to tell you two things: 1)In the last line, i used to get an error that:"Syntax error,insert";" to complete FieldDeclaration. 2)I inserted";", and saved it , then i get an error with the if, and else if lines that "button1 cannot be resolved into a variable" and "button2 cannot be resolved into a variable" respectively.

My main.xml code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button android:id="@+id/button1" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="@string/s1" 
    android:onClick="b1"/>
<Button android:id="@+id/button2" 
    android:layout_height="wrap_content" 
    android:text="Belladona" 
    android:layout_width="fill_parent" 
    android:onClick="b2"/>

</LinearLayout>

Please can anyone help me. Thanks in advance.

6 Answers 6

2

Well, button1 and button2 don't exist. You need to put R.id. before each of them.

Sign up to request clarification or add additional context in comments.

1 Comment

oh it was such a simple thing. Thank you
0

Just use this condition:

if (v.getId() == R.id.button1) { /* ... */ }

You can even use switch with view ids if you have more controls.

Comments

0

Here is your java file

    import android.app.Activity;
    import android.os.Bundle;
    import android.app.AlertDialog;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

public class Trial extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1 = findViewById(R.id.button1);
        Button b2 = findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button1) {
            new AlertDialog.Builder(v.getContext())
                    .setTitle("Paracettamol")
                    .setMessage("This medicine is generally used to cure Fever")
                    .setNeutralButton("OK", null).show();
        } else if (id == R.id.button2) {
            new AlertDialog.Builder(v.getContext())
                    .setTitle("sertraline")
                    .setMessage(
                            "This medicine is generally used to cure Head aches")
                    .setNeutralButton("OK", null).show();
        }

    }
}

Here is your XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button android:id="@+id/button1" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="@string/s1" 
    />
<Button android:id="@+id/button2" 
    android:layout_height="wrap_content" 
    android:text="Belladona" 
    android:layout_width="fill_parent" 
    />

</LinearLayout>

I have edited both of your files.. Hope it will help you.

Comments

0

You can try this one:

public class Trial extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1 = (Button)findViewById(R.id.button1); 
        Button b2 = (Button)findViewById(R.id.button2);t

        b1.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
            if(b1.getText().equals("button1"))   //No need to check this condition 
            {
            //Your logic
            }
            }
    });


        b2.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
            if(b2.getText().equals("button2"))   //No need to check this condition 
            {
            //Your logic
            }
            }
    });

}

Comments

0

Try this code to solve your problem

 public class ButtonCheck extends Activity {
   /** Called when the activity is first created. */
 View b1,b2;
 @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     b1 = (View)findViewById(R.id.button1);
    b1.setOnClickListener(yourListener);  
  b2 =(View) findViewById(R.id.button2);
    b2.setOnClickListener(yourListener); 

}
View.OnClickListener yourListener=new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v ==b1) {
            new AlertDialog.Builder(v.getContext())
                    .setTitle("Paracettamol")
                    .setMessage(
                            "This medicine is generally used to cure Fever")
                    .setNeutralButton("OK", null).show();
        } else if (v ==b2) {
            new AlertDialog.Builder(v.getContext())
                    .setTitle("sertraline")
                    .setMessage(
                            "This medicine is generally used to cure Head aches")
                    .setNeutralButton("OK", null).show();
        }


    }
};
}

and hello if you get solved please check right and vote it up.

1 Comment

@Tejas Tamkahne just click on the right side of this answer where you can see up arrow so just up it.ok
0

Instead of writing v == button1 which will always return false since both of them are different objects, you can use the equals method like this

if(v.equals(b1))
{
    ...................
}
else if(v.equals(b2))
{
    ................
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.