【问题标题】:findviewbyid() returns null in custom Adapter in Eclipsefindviewbyid() 在 Eclipse 的自定义适配器中返回 null
【发布时间】:2011-03-05 00:34:39
【问题描述】:

欢迎来到另一个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>

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

【问题讨论】:

    标签: android eclipse android-listview android-adapter


    【解决方案1】:

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

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

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

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

    RuntimeException with Android OptionsMenu

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

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

    感谢您的回答!

    【讨论】:

    • Chris -- 您是否将项目用作库?我在 Eclipse 中有类似的“更新”问题。哦,日食!
    • 是的,我确实有一些我写的库被引用为一个项目。
    【解决方案2】:

    尝试使用 /> 而不是 ...>

    终止 ImageView XML 条目

    如果不是这样,那么尝试一个一个地删除每个属性(如 layout_gravity 等)

    祝你好运。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 感谢您的回答!这并没有解决我的问题,但它确实是一个好主意,因为它很整洁。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多