button name cannot be resolved into a variable
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.
Well, button1 and button2 don't exist. You need to put R.id.
before each of them.
Just use this condition:
if (v.getId() == R.id.button1) { /* ... */ }
You can even use switch
with view ids if you have more controls.
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.
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
}
}
});
}
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.
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))
{
................
}
精彩评论