Android: Cannot cast from View to Gallery

我的梦境 提交于 2019-12-10 23:56:14

问题


I'd like to make a Gallery in android. In order to set the adapter in need to get the gallery which I defined in my xml file. Im doing that as: Gallery g = (Gallery) findViewById(R.id.gallery1); But with this code I can't compile my project, since I get the Error "Cannot cast from View to Gallery". My xml-file looks like this: "

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
 <Gallery android:id="@+id/gallery1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="bottom"
 />
</LinearLayout>

Anybody knows how to solve my problem? Thanks a lot


回答1:


Your code looks fine. Check if you have the correct import for Gallery

import android.widget.Gallery;



回答2:


I think you just created the Object instance for Gallery. Gallery is a AdapterView. So you must set Adapter for the Content. please check this sample



来源:https://stackoverflow.com/questions/3202176/android-cannot-cast-from-view-to-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!