0

I am using Butter Knife in my project along with the Material Design Libraries for the UI.

When attempting to create buttons using the Material Design Library i get a 'Class Cast Exception' because of the usage of Butter Knife.

Is there a way to fix this?

MainActivity.Java

@Bind(R.id.switch1) Switch switch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ButterKnife.bind(this);
}

XML Switch Layout

<com.gc.materialdesign.views.Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/switch1"
    android:layout_centerHorizontal="true" />

Errors

Caused by: java.lang.RuntimeException: Unable to bind views for com.example.MainActivty

Caused by: java.lang.ClassCastException:com.gc.materialdesign.views.Switch cannot be cast to android.widget.Switch
Perfect_Comment
  • 165
  • 1
  • 2
  • 15

2 Answers2

1

You are importing the wrong Switch - instead of importing android.widget.Switch, you need to import com.gc.materialdesign.views.Switch and use that class when defining your switch variable.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I literally just figured it out, i thought the library was already imported! Stupid me. Thank you for for your answer i really appreciate it. – Perfect_Comment Feb 20 '16 at 18:09
0

Remove the line

import android.widget.Switch;

and replace it with

import com.gc.materialdesign.views.Switch;

You've simply chosen to import the wrong Switch class.

Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127