android gridview始终为空
在我的活动中,我有: DrawView是一种自定义相对布局 此RelativeLayout应该存在一个自定义导航栏和一个自定义GridView 此处将自定义导航栏添加到DrawView:android gridview始终为空,android,android-layout,Android,Android Layout,在我的活动中,我有: DrawView是一种自定义相对布局 此RelativeLayout应该存在一个自定义导航栏和一个自定义GridView 此处将自定义导航栏添加到DrawView: final LayoutInflater factory = getLayoutInflater(); final View textEntryView = factory.inflate(R.layout.multiplechoice, null); com.lernapp.s
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.multiplechoice, null);
com.lernapp.src.Views.NavigationBarTest navigation = (com.lernapp.src.Views.NavigationBarTest)textEntryView.findViewById(R.id.navigationbar);
RelativeLayout parent = (RelativeLayout)navigation.getParent();
parent.removeAllViews();
drawView.addView(navigation);
我想对GridView做一些类似的事情
我不明白,我的CustomGridView总是空的,这是为什么
MyCustomGridView grid = (MyCustomGridView)findViewById(R.id.gridview);
xml:
编辑:
正如Heinrisch所说,在使用findViewById()之前需要调用setContentView。 为什么不将自定义相对布局添加到布局xml中?通常你不需要自己夸大观点
<com.lernapp.src.Views.DrawView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.lernapp.src.Views.MyCustomGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:columnWidth="160dp"
android:listSelector="@android:color/transparent"
android:numColumns="auto_fit" />
</com.lernapp.src.Views.DrawView>
正如Heinrisch所说,在使用findViewById()之前需要调用setContentView。 为什么不将自定义相对布局添加到布局xml中?通常你不需要自己夸大观点
<com.lernapp.src.Views.DrawView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.lernapp.src.Views.MyCustomGridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:columnWidth="160dp"
android:listSelector="@android:color/transparent"
android:numColumns="auto_fit" />
</com.lernapp.src.Views.DrawView>
将这一行添加到onCreate中,一切都会好起来:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_name_of_the_layout_for_this_activity);
更清楚的是,它是
setContentView(R.layout.the_name_of_the_layout_for_this_activity);
您应该添加到onCreate,并且必须在尝试执行findViewById()之前添加它。将这一行添加到onCreate,一切都会很好:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.the_name_of_the_layout_for_this_activity);
更清楚的是,它是
setContentView(R.layout.the_name_of_the_layout_for_this_activity);
您应该添加到onCreate,并且必须在尝试执行findViewById()之前添加它。您有两个选项
findViewById
。通过使用setContentView()
,您要查找的视图必须“在屏幕上”。没有其他办法,这就是函数findviewbyd
的作用setContentView()。如何做到这一点的示例很简单,但请查看并提供一些示例
findViewById
。通过使用setContentView()
,您要查找的视图必须“在屏幕上”。没有其他办法,这就是函数findviewbyd
的作用setContentView()。如何做到这一点的示例很简单,但请查看并提供一些示例
在调用findViewById之前,需要将ContentView设置为布局。你能发布活动代码吗?在主帖子中添加为编辑,但我如何在这里发布?因为我有一个自定义的DrawView,它是一个自定义的相对布局,在这个布局中我想添加GridView…在调用findViewById之前,您需要将ContentView设置到布局中。你能发布活动代码吗?在主帖子中添加为编辑,但我如何在这里发布?因为我有一个自定义的DrawView,它是一个自定义的相对布局,在这个布局中我想添加GridView…添加到我的Layout.xml是什么意思?添加到我的Layout.xml是什么意思?但是我没有这个活动的布局,这个布局存在于DrawView中,这是一个相对的例子,其中所有内容都是构建的。您是否可以更新这个问题,以清楚地显示使用了什么布局以及它们的名称:)仍然不清楚“xml:”在何处使用,由谁使用,我的假设是,您在这里展示的唯一活动都使用了它。如果是这样,那么活动必须手动或使用setContentView()方法将其膨胀。啊,天哪,你说得对!嗯。。。。好吧,我想我知道我做错了什么,我没有把它充气希望你能让它像你想的那样工作!别忘了投票给帮助我的ppl:)但是我没有这个活动的布局,DrawView的布局是存在的,这是一个相对的布局,所有东西都是构建的。你能更新这个问题来清楚地显示使用了什么布局以及它们被称为什么吗:)仍然不清楚“xml:”是在哪里被谁使用的,我的假设是,它被您在这里展示的唯一活动所使用。如果是这样,那么活动必须手动或使用setContentView()方法将其膨胀。啊,天哪,你说得对!嗯。。。。好吧,我想我知道我做错了什么,我没有把它充气希望你能让它像你想的那样工作!别忘了给帮助过你的人投票:)