2

欢迎来到另一个findviewbyid() returns 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;
}


}

我标记了返回 null 的行。

这是膨胀的 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>

你能看到任何明显的错误吗?一切对我来说似乎都是正确的。

4

3 回答 3

4

所以这个问题真的与日食有关。在我做了你提到的所有事情并用谷歌搜索我的手之后,我认为检查日食是否正确可能是一个好主意。

所以由于某种原因,向我显示的布局文件 eclipse 不是正在编译的文件。因此,出于这个原因,日食中的一切看起来都很好。

然后我直接在文件系统中查看了同一个文件,这仍然是旧文件。所以我刚刚刷新了我的 Eclipse 项目,瞧,旧文件出现在 Eclipse 中,我再次对其进行了编辑。那是我。我不知道为什么会发生这种情况,这让我感到害怕,日食可能会出现更多不透明的问题。

前几天我遇到了一个类似的问题。

请参阅带有 Android OptionsMenu 的 RuntimeException

问题出在哪里,R.java 文件在添加新的 id 资源后没有自动重新编译。

我现在要清理我的 Eclipse 安装并从头开始安装所有内容,以确保在我的安装过程中没有发生重大错误。

感谢您的回答!

于 2011-03-05T09:07:50.173 回答
1

尝试使用 /> 而不是 ...></ ImageView> 终止 ImageView XML 条目

如果不是这样,请尝试逐个删除每个属性(例如 layout_gravity 等)

祝你好运。

于 2011-03-05T01:11:14.253 回答
1

我很想不尝试使用 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);
         }

    }

我会保持这些行下面的其余代码相同。试试看!

于 2011-03-05T01:36:27.433 回答