0

Casting a class to a custom class. I know this has been asked time and again as seen Error referencing an inner class View in layout/main.xml and Error referencing an inner class View in layout/main.xml though sadly neither of these has helped me out. A quick background of what im trying to do.

I have a custom ListView, appropriately named CustomListView and a custom ImageView called CustomButton. I want to setup something with xml where the xml layout references the parent view, be it ListView or ImageView or what have you, and then is cast to the custom view.

Below is some of the relevant code for this as well as the stacktrace for you.

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"/>
    -->

Point in main code, where the view is inflated.

/**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);

Stacktrace:

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
Community
  • 1
  • 1
miversen33
  • 573
  • 5
  • 19

1 Answers1

0

you cannot cast ListView to CustomListView because CustomListView extends ListView and not the other way around. So in your Layout you have to change result_view to a customlistview

momor10
  • 478
  • 3
  • 11
  • Maybe it's a misunderstanding on my part. By extending list view, I thought I would be able to cast a list view to it, because it's inheritting the list view. Am I wrong in this thought process? – miversen33 Oct 19 '14 at 02:55
  • View view= new CustomListView(context); CustomListView casting= (CustomListView) view; It works like in this example because view is already a CustomListView. You cant Change it by casting. Your ListView is just a ListView. It's not a CustomListView. The Object lacks the properties of your CustomListView. The otherway around will work though. Because you your Custom ListView extends ListView, every CustomListView is a ListView by Definition, too. So it owns the properties of ListView and thats why a Cast from your CustomListView to ListView would work. – momor10 Oct 20 '14 at 14:59
  • I had it backwards. Thank you! – miversen33 Oct 20 '14 at 23:11