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.