Android topoche searchable spinner cannot cast 错误

Android topoche searchable spinner cannot cast error

不可转换类型;无法将 'android.view.View' 转换为 'com.toptoche.searchablespinnerlibrary.SearchableSpinner'

微调器 1 被分配给了错误的东西,您将它指定为视图而不是微调器 更简单的方法,

在你onCreate之前

Spinner spinnername; 

这样微调器就可以在整个 activity

期间调用

然后在你的onCreate下你做

spinnername=findViewById(R.id.nameofspinnerinxml);

并且在您的 xml 中,您必须使您的微调器像这样

  <com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:id="@+id/nameofspinnerinxml"
    style="@android:style/Widget.Holo.Light.Spinner"
    android:layout_width="match_parent"
    android:layout_height="51dp"
    tools:layout_editor_absoluteY="24dp" />

为您添加更完整的示例

Spinner spinner1;

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_project);

        spinner1=findViewById(R.id.spinnerid);

然后在 xml

  <com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:id="@+id/spinnerid"
    style="@android:style/Widget.Holo.Light.Spinner"
    android:layout_width="match_parent"
    android:layout_height="51dp"
    tools:layout_editor_absoluteY="24dp" />