((ImageView)viewHolder.image).setImageResource(id)

package com.tyut.gridview01;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PictureActivity extends Activity {

private String[] titles = {"MM1","MM2","MM3","MM4","MM5","MM6","MM7","MM8","MM9","MM10"};
private Integer[] ps =new Integer[]{R.drawable.g1,R.drawable.g2,R.drawable.g3,R.drawable.g4,R.drawable.g5,R.drawable.g6,R.drawable.g7,R.drawable.g8,R.drawable.g9,R.drawable.g10};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid);
    GridView grid = (GridView) findViewById(R.id.grid);
    grid.setAdapter(new PictureAdapter(titles, ps, this));
    grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Log.i("MainActivity", "position:"+position);
            Toast.makeText(PictureActivity.this, " "+position, Toast.LENGTH_SHORT).show();
        }
    });
}

}

class PictureAdapter extends BaseAdapter{

private static final String TAG = "MainActivity";

private LayoutInflater inflater;
private List<Picture> pictures;
public PictureAdapter(String[] titles ,Integer[] ps,Context context){
   super();
   pictures = new ArrayList<Picture>();
   inflater = LayoutInflater.from(context);
   for (int i = 0; i < ps.length; i++) {
    Picture p = new Picture(titles[i],ps[i]);
    pictures.add(p);
}
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    if (null !=pictures) {
        return pictures.size();
    }else{
    return 0;
    }
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return pictures.get(position);
}

@Override
public long getItemId(int id) {
    // TODO Auto-generated method stub
    return id;
}

@Override
public View getView(int position, View convertview, ViewGroup arg2) {
    // TODO Auto-generated method stub
    ViewHolder viewHolder;
    if (convertview == null) {
        convertview = inflater.inflate(R.layout.activity_griditem, null);
        viewHolder = new ViewHolder();
        Picture pic =pictures.get(position);
        int id = pic.getId();

## ((ImageView)viewHolder.image).setImageResource(id);
String t =pic.getTitle();
((TextView)viewHolder.title).setText(t);
convertview.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertview.getTag();
}
viewHolder.title.setText(pictures.get(position).getTitle());
viewHolder.image.setImageResource(pictures.get(position).getId());
Log.i(TAG, "<<<<<<<<"+position);
return convertview;

}
class ViewHolder{
    public TextView title;
    public ImageView image;
}   
public class Picture {
    private String title;
    private int id ;
    public Picture(String title,int id){
        this.title =title;
        this.id =id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

}
}
问题:

((ImageView)viewHolder.image).setImageResource(id);

说空指针异常

3个回答

@Override
public View getView(int position, View convertview, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.activity_griditem, null);
viewHolder = new ViewHolder();
viewHolder.title= convertview.findViewById(R.id.title);
viewHolder.image= convertview.findViewById(R.id.image);
convertview.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertview.getTag();
}
Picture pic =pictures.get(position);
int id = pic.getId();
String t =pic.getTitle();
viewHolder.title.setText(t);
viewHolder.title.setText(pictures.get(position).getTitle());
viewHolder.image.setImageResource(pictures.get(position).getId());
Log.i(TAG, "<<<<<<<<"+position);
return convertview;

qq_27807845
qq_27807845 谢谢
3 年多之前 回复
qq_27807845
qq_27807845 楼上正解
3 年多之前 回复

最常见的问题,报空,肯定是小数点前的东西有空,看你这样的,我提几点你检查一下输出看看是不是空了
1,viewHolder输出看是否为空,
2image是否为空,
出现原因无非就是在view中找不到这个控件,或者id不匹配

qq_27807845
qq_27807845 在debug下都有值的
3 年多之前 回复

打上断点在debug下运行显示有值,就是到了setImageResource(id);就报错

qq_36539304
接口的说法 viewHolder.image的id都没找到不报Null???
3 年多之前 回复
sinat_26761191
sinat_26761191 你说在debug下显示有值,但还是报这个空指针的错? sysout(viewHolder);sysout(viewHolder.image);
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐