0
I suppose it’s a simple problem, but I can’t visualize the solution. I created a floating button (FAB) in a Fragment on Android, and when trying to configure Clicklistener, I get a Nullpointer error. Follow 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 method setOnClickListener
in a null object.
Any hint is welcome. I appreciate.
You get
NullPointerException
, for thefloatingActionButton
is null. The XML it is declared refers to yourMainActivity
?– Vitor Henrique
Yes. But why is null? findViewById should not be the required assignment?
– Rodrigo Salvador