在 Runnable 中膨胀视图
我正在尝试编写代码,允许我在列表视图的顶部有 1-2 个视图,这些视图的排列方式与列表视图中的其他视图不同。我决定尝试使用 addHeaderView() 的方法来处理显示不同的 1-2 个视图。
我有两个 xml 布局文件,一个定义大多数列表视图视图所属的视图格式 (list_image_checkbox_row.xml),另一个用于定义标题视图 (catalog_featured_row.xml)。适配器构造函数使用 list_image_checkbox_row 作为构造函数中的资源。
我使用线程来设置适配器并加载视图。我能够以编程方式创建视图并使用 addHeaderView 将它们添加到列表视图(使用相同的图像资源),但是当我尝试在从布局 xml 文件膨胀的视图上使用 addHeaderView 时,出现错误。
handler.post(new Runnable() {
@Override
public void run() {
lv = (ListView)findViewById(R.id.sources_list);
Activity context = BrowseSourceActivity.this;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.catalog_featured_row, null);
ImageView feat_view = (ImageView) view.findViewById(R.id.image);
feat_view.setImageResource(R.drawable.arrow);
lv.addHeaderView(feat_view);
setListAdapter(array);
}
});
我收到强制关闭错误,并且无法在 logcat 中找到任何内容(当我昨天尝试调试时,我得到了 NullPointerException)。如果我注释掉 lv.addHeaderView(feat_view) 行,应用程序不会强制关闭我。以下是featured_catalog_row.xml 的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:padding="5dip">
<ProgressBar android:id="@+id/spinner"
android:indeterminate="true"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
/>
<ImageView android:id="@+id/image"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
对于如何让充气机工作有什么建议吗?
I am attempting to write code that would allow me to have 1-2 views at the top of a listview that are of a different arrangement than the other views in the listview. I decided to try the method of using addHeaderView() for the 1-2 views that will differ in display.
I have two xml layout files, one that defines the view format that most of the listview views will fall under (list_image_checkbox_row.xml) and one for the Header Views (catalog_featured_row.xml). The adapter constructor uses list_image_checkbox_row as its resource in the constructor.
I used a Thread to set the adapter and load the views. I am able to programmatically create views and use addHeaderView to at them to the listview (using the same image resource), but I get errors when I try to use addHeaderView on a view I have inflated from a layout xml file.
handler.post(new Runnable() {
@Override
public void run() {
lv = (ListView)findViewById(R.id.sources_list);
Activity context = BrowseSourceActivity.this;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.catalog_featured_row, null);
ImageView feat_view = (ImageView) view.findViewById(R.id.image);
feat_view.setImageResource(R.drawable.arrow);
lv.addHeaderView(feat_view);
setListAdapter(array);
}
});
I get a force close error and haven't been able to find anything in logcat (when I was trying to debug yesterday I got a NullPointerException). If I comment out the lv.addHeaderView(feat_view) line, the application does not force close on me. Here is the code for featured_catalog_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:padding="5dip">
<ProgressBar android:id="@+id/spinner"
android:indeterminate="true"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
/>
<ImageView android:id="@+id/image"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Any suggestions for how to get the inflater to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您应该将
lv
存储在实例变量中,而不是动态查找它。请注意,View
和Activity
都有一个名为findViewById
的方法,因此您的结果将取决于代码的组织方式。Perhaps you should store
lv
in an instance variable instead of looking it up dynamically. Note that bothView
andActivity
have a method calledfindViewById
, and so your results will depend on how your code is organized.