简体   繁体   中英

Fab button android onClick is crashing the app?

The (Floating action button) works but when I try to set it as a button it crashes the app?

Button MyButton = (Button) findViewById(R.id.fab);
MyButton.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    // Perform action on click
  }
});

The above crashes the app, but when I remove it everything is fine? Any ideas?

https://github.com/makovkastar/FloatingActionButton

The FloatingActionButton from this library extends ImageView which extends View:

https://github.com/makovkastar/FloatingActionButton/blob/master/library/src/main/java/com/melnykov/fab/FloatingActionButton.java

It is not a subclass of the Android Button. Cast instead to FloatingActionButton:

FloatingActionButton myButton = (FloatingActionButton) findViewById(R.id.fab);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM