getView(…)中GridView NullPointerException的自定义ImageAdapter

getView(…)中GridView NullPointerException的自定义ImageAdapter,gridview,typedarray,Gridview,Typedarray,我有一个名为AddZoneDialog的类,它扩展了DialogFragment。在这个类中,我试图用存储在名为arrays.xml的xml文件中的图像填充GridView 我使用TypedArray检索xml文件中的条目。我将该数组传递给自定义ImageAdapter(称为ZoneIconAdapter)以填充GridView。问题是,我一直在线上收到NullPointerException holder.iv.setImageResource(imgs.getResourceId(posit

我有一个名为AddZoneDialog的类,它扩展了DialogFragment。在这个类中,我试图用存储在名为arrays.xml的xml文件中的图像填充GridView

我使用TypedArray检索xml文件中的条目。我将该数组传递给自定义ImageAdapter(称为ZoneIconAdapter)以填充GridView。问题是,我一直在线上收到NullPointerException

holder.iv.setImageResource(imgs.getResourceId(position, -1));
为了使用getResourceId(…)测试TypedArray的使用情况,我在GridView外部的DialogFragment中放置了一个ImageView,并使用以下方法手动填充了一个图像:

(ImageView) iv = (ImageView) view.findViewById(R.id.ivIcon);
然后,我使用ZoneIconAdapter中使用的相同语法向其中添加图像:

iv.setImageResource(imgs.getResourceId(0, -1));
并显示正确的图像。 映像适配器位于名为com.my.project.model的包中,因此我导入了com.my.project.R 在适配器中,如果我尝试硬编码资源,它仍然会给出Null错误:

holder.iv.setImageResource(R.drawable.bed1);
我的代码如下: 类别-区域适配器:

public class ZoneIconAdapter extends BaseAdapter {
private static final String LOGTAG = "HOMECONTROL";

Context context;
TypedArray imgs;

public ZoneIconAdapter(Context c, TypedArray a){
    this.context = c;
    imgs = a;
}

@Override
public int getCount() {
    //return imgIds.length;
    return imgs.length();
}

@Override
public Object getItem(int index) {
    return imgs.getResourceId(index, -1);
}

@Override
public long getItemId(int index) {
    return index;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if(convertView == null){

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.zone_icon_list, parent, false);

        holder = new ViewHolder();
        holder.iv = (ImageView) convertView.findViewById(R.id.ivIcon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    Log.w(LOGTAG, "(ZoneIconAdapter) pos: "+ position + " drawable : " +          imgs.getDrawable(position) + " res: " + 
            imgs.getResourceId(position, -1));
    holder.iv.setImageResource(imgs.getResourceId(position, -1));  // ***NULL ERROR ***

    return convertView;
}

private class ViewHolder {
    ImageView iv;
}

}
类-AddZoneDialog.java

 public class AddZoneDialog extends DialogFragment {
private static final String LOGTAG = "HOMECONTROL";

public static final String EXTRA_ZONE_NAME = "homecontrol.zonename";
public static final String EXTRA_NAME_LIST = "homecontrol.namelist";
public static final String EXTRA_IMG_LIST = "homecontrol.imglist";

private ZoneList zoneList;  
private EditText etZoneName;
private GridView gvIcons;
TypedArray ids;


public AddZoneDialog newInstance(ZoneList zones){
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_NAME_LIST, zones);

    AddZoneDialog newDialog = new AddZoneDialog();
    newDialog.setArguments(args);

    return newDialog;
}

@Override
public Dialog onCreateDialog(Bundle savedState) {
    View v = getActivity().getLayoutInflater()
            .inflate(R.layout.frag_add_zone_dialog, null);

    zoneList =  (ZoneList) getArguments().getSerializable(EXTRA_NAME_LIST);
    gvIcons = (GridView) v.findViewById(R.id.gvZonesIcons);
    etZoneName = (EditText) v.findViewById(R.id.etZonesPopupName);
    etZoneName.requestFocus();

    ImageView iv = (ImageView) v.findViewById(R.id.ivIcon);

    ZoneIconAdapter iconAdapter = new ZoneIconAdapter(getActivity(), ids);
    gvIcons.setAdapter(iconAdapter);

    ids = getActivity().getResources().obtainTypedArray(R.array.zone_icons);
    iconAdapter.notifyDataSetChanged();
    Log.w(LOGTAG, "SIZE OF IDS: " + ids.length());

    iv.setImageResource(ids.getResourceId(0, -1));  // *****THIS WORKS*****

    return new AlertDialog.Builder(getActivity())
        .setView(v)
        .setTitle(R.string.zones_add_dialog_title)
        .setPositiveButton(R.string.ok, new AlertDialog.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                sendResult(Activity.RESULT_OK);
            }
        })
        .setNegativeButton(R.string.cancel, null)
        .create();
}



@Override
public void onActivityCreated(Bundle bundle) {
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    super.onActivityCreated(bundle);
}

public void sendResult(int resultCode){
    String newName = etZoneName.getText().toString();
    if ((newName.length() < 1) || (newName == null) || (newName == "")){
        Toast.makeText(getActivity(), "You must enter a name.", Toast.LENGTH_SHORT).show();
        return;
    }
    else{
        if (nameOnList(newName)){
            Toast.makeText(getActivity(), "That name is already in use.  Please enter another name.",
                    Toast.LENGTH_LONG).show();
            return;
        }
    }

    if(getTargetFragment() == null)
        return;

    Intent i = new Intent();
    i.putExtra(EXTRA_ZONE_NAME, newName);

    getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
}

boolean nameOnList(String name){
    for (int i = 0; i < zoneList.size(); i++){
        if (zoneList.get(i).getName().equals(name))
            return true;
    }
    return false;
}

@Override
public void onStop() {
    ids.recycle();
    super.onStop();
}
}
看看GridView是否只填充了一个TextView,它工作得很好。
我整天都在做这件事,已经查遍了我能找到的所有stackoverflow条目,我累坏了。我觉得这一定是我忽略了一些愚蠢的事情,但只是不确定。

嗯,这件事有点尴尬。在“自定义网格适配器”视图中,我输入了以下代码:

<ImageView
    android:name="@+id/..."

<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp" >

<TextView 
    android:id="@+id/tvZonesEnterName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/zones_enter_name"/>
<EditText
    android:id="@+id/etZonesPopupName"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:inputType="text"
    android:hint="@string/zones_name_hint"
    android:singleLine="true" />
<TextView 
    android:id="@+id/tvZonesSelectIcon"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/zones_select_icon" />
 <ImageView
    android:id="@+id/ivIcon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:scaleType="centerCrop" />

<GridView
    android:id="@+id/gvZonesIcons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_gravity="center_horizontal" />

</LinearLayout>
...
holder.tv = (TextView) convertView.findViewById(R.id.tvTest);
...
holder.tv.setText("image " + position);
<ImageView
    android:name="@+id/..."
<ImageView
    android:id="@+id/..."