简体   繁体   English

android:在相对布局内获取对gridview的引用

[英]android : get reference to gridview inside a relative layout

I have this layout 我有这个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/online_top_deals_color"
    android:orientation="vertical"
    android:showDividers="none" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <RelativeLayout
        android:id="@+id/gridHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button1" >

        <GridView
            android:id="@+id/topWebsitesGrid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:columnWidth="150dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

    </RelativeLayout>


</RelativeLayout>

When I try obtaining a reference to grid view 当我尝试获取对网格视图的引用时

View topWebsitesView = inflater.inflate(R.layout.fragment_online_top_websites, container, false);

        mTopWebsitesGrid = (GridView) topWebsitesView.findViewById(R.id.topWebsitesGrid);

I get ClassCastException error "Relativelayout cannot be cast to gridview" 我收到ClassCastException错误“ Relativelayout无法强制转换为gridview”

As per your code I think there is no problem. 根据您的代码,我认为没有问题。 I think R.java class does not build up automatically . 我认为R.java类不会自动建立。 Sometimes it happens. 有时候这种情况会发生。 First clean the project and run if you get any error then please let me inform. 首先清理项目并运行,如果遇到任何错误,请通知我。

Thanks!!! 谢谢!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM