【问题标题】:Casting a class to a custom class将类转换为自定义类
【发布时间】:2014-10-18 23:44:23
【问题描述】:

将类转换为自定义类。我知道这已经被一次又一次地问到了,正如Error referencing an inner class View in layout/main.xmlError referencing an inner class View in layout/main.xml 所见,但遗憾的是这些都没有帮助我。我正在尝试做的事情的快速背景。

我有一个自定义 ListView,适当地命名为 CustomListView 和一个自定义 ImageView,称为 CustomButton。我想用 xml 设置一些东西,其中 xml 布局引用父视图,无论是 ListView 还是 ImageView 或者你有什么,然后被转换到自定义视图。

下面是一些相关代码以及堆栈跟踪。

search_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#40FAEBD7">

   <RelativeLayout android:id="@+id/pullDown_bar"
       android:layout_width="match_parent"
       android:layout_height="30dp"
       android:layout_alignParentTop="true"
       android:layout_alignParentLeft="true"
       android:focusableInTouchMode="true"
       android:clickable="true"
       android:tag="BLADEBEAT_PRO_SWIPEUP_PARENTLAYOUT"
       android:background="@color/Black">

        <View android:id="@+id/pullDown_inner_bar"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="13dp"
            android:tag="BLADEBEAT_PRO_SWIPEUP_TOP_INNER_BAR"
            android:focusableInTouchMode="true"
            android:clickable="true"/>

   </RelativeLayout>

   <EditText android:id="@+id/search_bar"
        android:hint="Type Here To Search Your Device"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pullDown_bar"
        android:tag="BLADEBEAT_PRO_SWIPEUP_SEARCH_BAR"
        android:focusableInTouchMode="true"/>

   <RelativeLayout
       android:id="@+id/top_bar"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:background="@color/Cyan"
       android:layout_below="@id/search_bar"
       android:layout_alignParentLeft="true">

       <ImageView
           android:id="@+id/top_bar_left"
           android:layout_width="50dp"
           android:layout_height="match_parent"
           android:background="@drawable/textmessage"/>

  </RelativeLayout>


    <RelativeLayout
       android:id="@+id/bottom_bar"
       android:tag="BLADEBEAT_PRO_SWIPEUP_BOTTOM_BAR"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentBottom="true"
       android:background="@color/Pink"
       android:clickable="true">


        <ImageView
            android:id="@+id/bottom_bar_left"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_alignParentLeft="true"/>

        <ImageView
            android:id="@+id/bottom_bar_left_mid"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"/>

        <ImageView
            android:id="@+id/bottom_bar_mid"
            android:layout_width="50dp"
            android:layout_height="match_parent"
             android:layout_marginRight="5dp"/>

        <ImageView
            android:id="@+id/bottom_bar_right_mid"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"/>

        <ImageView
            android:id="@+id/bottom_bar_right"
            android:tag="BLADEBEAT_PRO_SWIPEUP_SETTINGS_BUTTON"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/gear"
            android:focusableInTouchMode="true"
            android:clickable="true"/>
    </RelativeLayout>

    <ListView
       android:id="@+id/result_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/top_bar"
       android:layout_above="@id/bottom_bar"
       android:visibility="invisible"
       android:tag="BLADEBEAT_PRO_SWIPEUP_RESULT_VIEW"
       android:focusableInTouchMode="true"
       android:clickable="true"/>

    <RelativeLayout
        android:id="@+id/content_filler"
        android:tag="BLADEBEAT_PRO_SWIPEUP_CONTENT_FILLER"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_bar"
        android:layout_above="@id/bottom_bar"
        android:background="@color/Silver"
        android:clickable="true"
        android:focusableInTouchMode="true">
    </RelativeLayout>

</RelativeLayout>

custom_button.xml

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

<!--
<bladebeat.pro.utils.swipeup.GUI.CustomButton  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="Settings Button"
    android:layout_gravity="center"/>
-->

<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="bladebeat.pro.utils.swipeup.GUI.CustomButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

custom_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<bladebeat.pro.utils.swipeup.GUI.CustomListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</bladebeat.pro.utils.swipeup.GUI.CustomListView>
<!--
   <bladebeat.pro.utils.swipeup.GUI.CustomListView xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
-->
<!--
<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="bladebeat.pro.utils.swipeup.GUI.CustomListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>
    -->

主代码中的点,视图被膨胀的地方。

/**assume that layout is search_view inflated correctly, as the rest of the views called work just fine*/ lView = (CustomListView) layout.findViewById(R.id.result_view);

堆栈跟踪:

10-18 23:21:04.925: E/AndroidRuntime(2046): FATAL EXCEPTION: main
10-18 23:21:04.925: E/AndroidRuntime(2046): Process: bladebeat.pro.utils.swipeup, PID: 2046
10-18 23:21:04.925: E/AndroidRuntime(2046): java.lang.RuntimeException: Unable to start activity ComponentInfo{bladebeat.pro.utils.swipeup/bladebeat.pro.utils.swipeup.MainActivity}: java.lang.ClassCastException: android.widget.ListView cannot be cast to bladebeat.pro.utils.swipeup.GUI.CustomListView
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.os.Looper.loop(Looper.java:136)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at java.lang.reflect.Method.invokeNative(Native Method)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at java.lang.reflect.Method.invoke(Method.java:515)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at dalvik.system.NativeStart.main(Native Method)
10-18 23:21:04.925: E/AndroidRuntime(2046): Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to bladebeat.pro.utils.swipeup.GUI.CustomListView
10-18 23:21:04.925: E/AndroidRuntime(2046):     at bladebeat.pro.utils.swipeup.MainActivity.createView(MainActivity.java:204)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at bladebeat.pro.utils.swipeup.MainActivity.loadActivity(MainActivity.java:147)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at bladebeat.pro.utils.swipeup.MainActivity.onCreate(MainActivity.java:134)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.Activity.performCreate(Activity.java:5231)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-18 23:21:04.925: E/AndroidRuntime(2046):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-18 23:21:04.925: E/AndroidRuntime(2046):     ... 11 more

【问题讨论】:

  • result_viewListView 而不是 CustomListView,你期待什么?

标签: java android xml android-layout listview


【解决方案1】:

您不能将 ListView 转换为 CustomListView,因为 CustomListView 扩展 ListView 而不是相反。因此,在您的布局中,您必须将 result_view 更改为 customlistview

【讨论】:

  • 也许这是我的误解。通过扩展列表视图,我认为我可以将列表视图投射到它,因为它继承了列表视图。我在这个思考过程中错了吗?
  • 查看视图= new CustomListView(context); CustomListView cast= (CustomListView) 视图;它的工作方式与此示例中的一样,因为视图已经是一个 CustomListView。你不能通过铸造来改变它。您的 ListView 只是一个 ListView。它不是 CustomListView。该对象缺少您的 CustomListView 的属性。反之亦然。因为您的自定义 ListView 扩展了 ListView,所以每个 CustomListView 也是定义的 ListView。所以它拥有 ListView 的属性,这就是为什么从您的 CustomListView 到 ListView 的 Cast 会起作用的原因。
  • 我倒过来了。谢谢!
猜你喜欢
  • 2022-01-22
  • 2013-04-01
  • 2016-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多