Miroslav Marekovič Miroslav Marekovič - 17 days ago 6
Android Question

Floating Button Start New Activity

I have a problem with starting new activity via Floating Button. When I add next code into my onCreate method, app will crash down after start.

FloatingActionButton addEvent = (FloatingActionButton) this.findViewById(R.id.float_btn_add_event);
addEvent.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(IndexActivity.this, AddEventActivity.class));
}
});


Here is my Floating Button.

<android.support.design.widget.FloatingActionButton
android:id="@+id/float_btn_add_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@mipmap/ic_add"
app:layout_anchor="@id/listView1"
app:layout_anchorGravity="bottom|right|end"
android:layout_marginRight="13dp"
android:layout_marginBottom="12dp"
app:backgroundTint="#ffffff"
android:layout_marginEnd="13dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addEvent" />


I tried second solution. I created next activity, but result is same.

public void addEvent(View view) {
startActivity(new Intent(this, AddEventActivity.class));
}


Be patient pleas, I am a beginner. :)

Answer

Remove this import from your project com.melnykov.fab.FloatingActionButton and import this android.support.design.widget.FloatingActionButton.

Also double check that you have register your AddEventActivity in manifest.