I suppose it's a simple problem, but I can not see the solution. I created a Floating Button (FAB) in a Fragment on Android, and when I try to configure the ClickListener, I get a NullPointer error. Here are the codes:
xml:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_margin="16dp"
android:clickable="true"/>
MainActivity.java
FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent criar = new Intent(MainActivity.this, Fragment1.class);
startActivity(criar);
}
}
The error message says that the code tries to call the setOnClickListener
method on a null object.
Any tips are welcome. Thanks.