gdubs
gdubs

Reputation: 2754

Butterknife onclick does not trigger the method in a fragment

I have the following

public class InventoryFragment extends Fragment {
    @BindView(R.id.fab_fragment_inventory)FloatingActionButton mFab;
private Unbinder unbinder;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_inventory, container, false);

        unbinder = ButterKnife.bind(this, view);

return view
}

@OnClick(R.id.fab_fragment_inventory)
    public void newItemDialog(){
        // do stuff
    }

And when I put the method on the xml, it throws this error

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_fragment_inventory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:elevation="4dp"
android:onClick="newItemDialog"
        android:src="@drawable/vector_plus"
        app:layout_anchorGravity="bottom|right|end"/>



> java.lang.IllegalStateException: Could not find a method
> newItemDialog(View) in the activity class
> simpleinventory.simpleinventory.activities.InventoryActivity for
> onClick handler on view class
> android.support.design.widget.FloatingActionButton with id
> 'fab_fragment_inventory'

Here's my gradle

dependencies {
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
    compile 'com.android.support:cardview-v7:23.0.0'

    compile 'com.jakewharton:butterknife:8.1.0'
}

EDIT Failed to mention that I did not put on the onclick at first. Without it nothing happens.

Upvotes: 2

Views: 1127

Answers (1)

mpostal
mpostal

Reputation: 355

You are using onClick with the ButterKnife, it must not be in the xml file. So just remove the android:onClick="newItemDialog" from the xml.

EDIT

You need to add this in the first line of your build.gradle.

apply plugin: 'android-apt'

And in you project build.gradle you need to add the dependency:

buildscript {
   repositories {
      mavenCentral()
   }

   dependencies {
      classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
   }}

You can see it in the github project.

https://github.com/JakeWharton/butterknife#download


Upvotes: 1

Related Questions