将类转换为自定义类

时间:2014-10-18 23:44:24

标签: java android xml android-layout listview

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

我有一个自定义ListView,适当地命名为CustomListView和一个名为CustomButton的自定义ImageView。我想用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

1 个答案:

答案 0 :(得分:0)

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