gpt4 book ai didi

java - 为什么我不能将 View 类型的对象转换为 KeyboardView

转载 作者:行者123 更新时间:2023-12-01 08:05:12 26 4
gpt4 key购买 nike

我定义了一个像这样的自定义KeyboardView:

    public class MyKeyboardView extends KeyboardView
{
public MyKeyboardView(Context context, AttributeSet attrs)
{
super(context, attrs);
...

在我的代码中的某个地方我有:

    MyKeyboardView myKbView = (MyKeyboardView) myViewGroup.findViewWithTag("MyKeyboardView");

其中 myViewGroup 来自膨胀以下 xml 文件:

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<android.gesture.GestureOverlayView
android:id="@+id/gestureOverlayView"
android:gestureColor="#00000000"
android:layout_width="match_parent"
android:tag="MyGestureOverlayView"
android:layout_height="wrap_content">

<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard"
android:keyBackground="@drawable/orange_keyboard_keys_background"
android:keyTextColor="#ff8710"
android:background="#4c4c4c"
android:tag="MyKeyboardView"
android:keyTextSize="21sp"
android:keyPreviewLayout="@layout/key_preview_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</android.gesture.GestureOverlayView>

</LinearLayout>

这就是 Logcat 所说的:

    03-09 22:52:29.274: E/deliKey(21719): java.lang.ClassCastException:
android.inputmethodservice.KeyboardView cannot be cast to
com.parasum.delikey.MyKeyboardView

为什么?

最佳答案

android.inputmethodservice.KeyboardView 和 com.parasum.delikey.MyKeyboardView 确实都是 View 的后代,但都不是另一个的后代。它类似于 View 和字符串。它们都是 Object 的后代,但不能在它们之间进行转换。

关于java - 为什么我不能将 View 类型的对象转换为 KeyboardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com