findviewbyid()在Eclipse中的自定义适配器中返回null
欢迎来到另一个findviewbyid() returns null
的问题!findviewbyid()在Eclipse中的自定义适配器中返回null
我写了一个适配器类,用于将数组显示到ListView中。并猜测findviewbyid()返回null。 (我检查了关于这个主题的所有其他问题,但他们与我的问题没有关系)。
因此,这里是我的适配器类别:
package de.kosmowski.discogs.wants;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.kosmowski.discogs.model.Want;
public class WantAdapter extends ArrayAdapter<Want> {
private ArrayList<Want> items;
public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Want w = items.get(position);
Log.d("WantAdapter", "want:" + w);
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
{
Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
v = vi.inflate(R.layout.releaselistitem, null);
}
else
{
v = vi.inflate(R.layout.lplistitem, null);
}
}
if (w != null) {
ImageView icon = (ImageView) v.findViewById(R.id.typeicon); //Returns NULL at this line
TextView tt = (TextView) v.findViewById(R.id.text1);
if(w.getType() != null)
{
if(w.getType().equals(Want.TYPE_ARTIST))
{
icon.setImageResource(R.drawable.resulttype_artist);
}
else if(w.getType().equals(Want.TYPE_RELEASE))
{
icon.setImageResource(R.drawable.resulttype_lp);
}
else if(w.getType().equals(Want.TYPE_MASTER))
{
icon.setImageResource(R.drawable.resulttype_lp_master);
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
if (tt != null) {
tt.setText(w.getName()); }
}
return v;
}
}
标志着我在那里返回空行。
这里是膨胀的xml文件(这两个lplistitem和releaselistitem目前完全一样):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:scaleType="center" android:layout_gravity="center_vertical" android:src="@drawable/defaultresult" android:id="@+id/typeicon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="16px"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
</LinearLayout>
你能看到任何明显的错误?一切似乎都适合我。
所以这个问题真的和日食有关。在我做了你所提到的所有事情之后,把我的手放在骨骼上,我认为这可能是一个好主意,以检查日食是否正确。
因此,出于某种原因,向我显示的布局文件eclipse并不是正在编译的文件。所以出于这个原因,在月蚀中一切看起来都很好。
然后我直接在文件系统中查看了同一个文件,这仍然是旧的。 所以我刚刚刷新了我的eclipse项目,瞧这个旧文件出现在eclipse中,我再次编辑它。那是。我不知道为什么发生这种情况,这让我感到恐惧,可能会有更多的这些与日食不相关的问题。
前几天我遇到了一个类似于它的问题。
看到RuntimeException with Android OptionsMenu
那里的问题是,该R.java文件并没有添加新的ID资源后,全自动重新编译。
我现在要清理我的eclipse安装,并从头开始安装所有东西,以确保在安装过程中没有发生重大错误。
感谢您的回答!
尝试终止与ImageView的XML条目/>而不是...> </ImageView的>
如果不是,那么请尝试删除每个属性一个接一个的(如layout_gravity等)
祝你好运。
我会试图不要尝试和有一个LayoutInflater变量,但尝试膨胀布局在一行。此外,我会试图将上下文存储为私有变量,以确保正确使用上下文。因此,继承人什么,我会尝试在你的代码做的是不同的:
public class WantAdapter extends ArrayAdapter<Want> {
private ArrayList<Want> items;
private Context mContext;
public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
super(context, textViewResourceId, items);
this.items = items;
mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Want w = items.get(position);
Log.d("WantAdapter", "want:" + w);
if (v == null) {
if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
{
Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
v = LayoutInflater.from(mContext).inflate(R.layout.releaselistitem, null);
}
else
{
v = LayoutInflater.from(mContext).inflate(R.layout.lplistitem, null);
}
}
我会继续下面这些行相同的代码的其余部分。试试看!
感谢您的回答!这并没有解决我的问题,但它确实是一个好主意,因为它很整洁。 – Chris 2011-03-05 08:55:37
Chris - 你是否将项目用作图书馆?我在Eclipse中有类似的'更新'问题。哦,Eclipse! – pjama 2011-03-06 00:44:54
是的,我确实有一些我写作的项目引用的库。 – Chris 2011-03-06 14:25:12