java.lang.ClassCastException 仅当 SearchView 出现在布局中时才在 CardView 上

java.lang.ClassCastException on CardView only when SearchView is present in layout

我有一个关于 android 布局的问题,我无法解决。我有一个包含很多元素的布局片段,尤其是这个 CardView 元素

<androidx.cardview.widget.CardView
                    android:id="@+id/search_button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="20dp"
                    app:cardBackgroundColor="@color/chartMax"
                    app:cardCornerRadius="20dp"
                    app:cardElevation="2dp">

                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:padding="5dp"
                        android:text="Cerca"
                        android:textAlignment="center"
                        android:textColor="#fff"
                        android:textSize="23sp" />
            </androidx.cardview.widget.CardView>

我在片段中访问这个元素

private lateinit var searchButton: CardView
searchButton = view.search_button

一切正常,我可以正常使用

当我向片段添加一个普通的 SearchView 时,问题就开始了。

<androidx.appcompat.widget.SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

当我 运行 应用程序在这条指令上崩溃时

searchButton = view.search_button

这是堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.atoka.mobile, PID: 31291
    java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to androidx.cardview.widget.CardView
        at io.atoka.mobile.fragments.SearchFragment.getViewElements(SearchFragment.kt:57)
        at io.atoka.mobile.fragments.SearchFragment.onCreateView(SearchFragment.kt:73)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
        at android.app.Activity.performStart(Activity.java:7157)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

就像我的 CardView 是一个 AppCompatImageView 而不是。 当我删除 SearchView 时,应用程序正常工作。

有人知道解决办法吗?关于这个特殊情况是否有任何错误?

谢谢

你的问题是因为你给你的 cardview 起的名字 search_button 已经默认定义为 androidx.appcompat.widget.SearchView class 如下:

 final ImageView mSearchButton;
 mSearchButton = findViewById(R.id.search_button);

因此,当您尝试通过绑定获取它时,它会与 SearchView class search_button 参数发生冲突

尝试将您的卡片视图名称更改为其他名称,然后尝试,