我有一个关于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类中定义如下:
final ImageView mSearchButton;
mSearchButton = findViewById(R.id.search_button);
因此,当您试图从绑定中获取它时,它会与SearchView类search_button
参数发生冲突
尝试将您的卡片视图名称更改为其他名称,然后尝试,