2

content_main.xml

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/beer_btn"
    android:id="@+id/button"
    android:layout_below="@+id/color"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onClickFindBeer"/>

JAVA FILE

public class FindBeerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
}
public void onClickFindBeer(View view){
    TextView brands=(TextView)findViewById(R.id.brands);
    Spinner color=(Spinner)findViewById(R.id.color);
    String beerType=String.valueOf(color.getSelectedItem());
    brands.setText(beerType);
}

Hello there. While running this code The Button action is not working.. The app crashes. I cant find any error. I got this code from Head First android development tutorial.

Somebody please find the error and help me

here is the error log

05-26 09:34:30.929 19451-19451/com.example.devan.layouttut E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.devan.layouttut, PID: 19451
                                                                         java.lang.IllegalStateException: Could not find method onClickFindBeer(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                             at android.view.View.performClick(View.java:5204)
                                                                             at android.view.View$PerformClick.run(View.java:21153)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Dfx
  • 65
  • 1
  • 8

1 Answers1

0

Your question is not very ideal for SO and there are a lot of tutorials here and there regarding how to work with button click in Android.

But anyway, here's how you can implement the onClick on a button.

In your FindBeerActivity remove the onClickFindBeer function.

public class FindBeerActivity extends Activity {

    // Get the Button variable first
    private Button myButton;
    private TextView brands;
    private Spinner color;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        myButton = (Button) findViewById(R.id.my_button);
        brands = (TextView) findViewById(R.id.brands);
        color = (Spinner) findViewById(R.id.color);

        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String beerType=String.valueOf(color.getSelectedItem());
                brands.setText(beerType);
                Toast.makeText(this,"Clicked", Toast.LENGTH_LONG).show();
            }
        });
    }
}

From your content_main.xml remove the onClick="onClickFindBeer" too.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/beer_btn"
    android:id="@+id/my_button"
    android:layout_below="@+id/color"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

There are of course other ways to implement onClick of a button, but I think this is the simplest.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98